To scale a complex query, I think I need my plugin to add indexes on core tables. What things should I consider?

Re: To scale a complex query, I think I need my plugin to add indexes on core tables. What things should I consider?

by Hubert Chathi -
Number of replies: 0

Matt, that's a very interesting result.  Could you, just for curiosity's sake, try the query again without the combined index, but with a new index on mdl_question_attempt_steps, on just the state column?  If I'm reading the EXPLAIN correctly, then it should run just as fast.

As I understand EXPLAINs, the order that it's given represents the order that MySQL processes the tables.  So without the index (in the first EXPLAIN), for the given data set, MySQL decides that it's best to start by querying mdl_course_modules (course_modules) for module=14, then join with mdl_quiz (moduletable), and so on.  It doesn't get to mdl_question_attempt_steps (sub) until second-last.

With the new index, it starts by querying mdl_question_attempt_steps (sub) for state = 'needsgrading', and then proceding from there.  At this point, the 'state' part of the index is the only part that's used, since it isn't matching questionattemptid.