I'm looking at a moodle postgresql database with about 4000 courses, 300K mdl_context records and 200 course categories. The following query gets executed on pretty much every page load I've seen so far and even operating off of fully cached data consistently takes around 200ms. Because of the frequency with which the query gets executed, with most other common queries falling into the sub-millisecond territory, I'd like to find a way to make it faster. I don't know that much can be done with indexing, as it's not a problem with seeking in any one of those tables. I'd think making a view out of the query and/or using pgmemcache would result in being able to run it substantially more quickly (but would require application code changes). Are there any suggested database optimizations that would be transparent to the application and would help this query run faster? Is it something that's already been addressed in current development (I'm using version 2.2)? Am I thinking it's a bigger problem than it actually is? Right now it seems to have a substantial impact on the number of req/s I can handle, but that may be due to testing against pages that aren't normally hit as heavily.
Here's the query:
SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category, cat.path AS categorypath , ctx.id AS ctxid, ctx.path AS ctxpath, ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel, ctx.instanceid AS ctxinstance
FROM mdl_course c
LEFT JOIN mdl_context ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
LEFT JOIN mdl_course_categories cat ON cat.id=c.category
WHERE c.id <> $1
ORDER BY c.sortorder ASC LIMIT 20 OFFSET 0
2012-04-20 22:50:47 UTC DETAIL: parameters: $1 = '1'