Skip to content

Commit

Permalink
Improve performance of statistics queries
Browse files Browse the repository at this point in the history
Without this, the many sub-queries in stats calculations that JOIN the
question_references table dont hit any of the indexes on that table. For
sites with large question_references tables - this can be horrible for
performance as it ends up doing a seqscan on that table.

By adding the usingcontextid field, we instead get indexscan's.
  • Loading branch information
aolley committed Jul 15, 2024
1 parent 3c2ae9b commit 9c11882
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions classes/statistics_calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private static function get_attempt_stat_joins($cmid, $groupid, $excluderoles =
JOIN {question_references} qr ON qr.itemid = sqq.id
AND qr.component = 'mod_studentquiz'
AND qr.questionarea = 'studentquiz_question'
AND qr.usingcontextid = :contextid1
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid AND qv.version = (
SELECT MAX(version)
Expand Down Expand Up @@ -163,6 +164,7 @@ private static function get_attempt_stat_joins($cmid, $groupid, $excluderoles =
JOIN {question_references} qr ON qr.itemid = sqq.id
AND qr.component = 'mod_studentquiz'
AND qr.questionarea = 'studentquiz_question'
AND qr.usingcontextid = :contextid2
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid AND qv.version = (
SELECT MAX(version)
Expand Down Expand Up @@ -197,6 +199,7 @@ private static function get_attempt_stat_joins($cmid, $groupid, $excluderoles =
JOIN {question_references} qr ON qr.itemid = sqq.id
AND qr.component = 'mod_studentquiz'
AND qr.questionarea = 'studentquiz_question'
AND qr.usingcontextid = :contextid3
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid AND qv.version = (
SELECT MAX(version)
Expand Down Expand Up @@ -231,6 +234,7 @@ private static function get_attempt_stat_joins($cmid, $groupid, $excluderoles =
JOIN {question_references} qr ON qr.itemid = sqq.id
AND qr.component = 'mod_studentquiz'
AND qr.questionarea = 'studentquiz_question'
AND qr.usingcontextid = :contextid4
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid AND qv.version = (
SELECT MAX(version)
Expand Down Expand Up @@ -259,6 +263,7 @@ private static function get_attempt_stat_joins($cmid, $groupid, $excluderoles =
JOIN {question_references} qr ON qr.itemid = sqq.id
AND qr.component = 'mod_studentquiz'
AND qr.questionarea = 'studentquiz_question'
AND qr.usingcontextid = :contextid5
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid AND qv.version = (
SELECT MAX(version)
Expand Down Expand Up @@ -303,6 +308,7 @@ private static function get_attempt_stat_joins($cmid, $groupid, $excluderoles =
* @return array
*/
private static function get_attempt_stat_joins_params($cmid, $quantifiers = null, $userid = null): array {
$contextid = \context_module::instance($cmid)->id;
$params = [
'cmid1' => $cmid,
'cmid2' => $cmid,
Expand All @@ -311,6 +317,11 @@ private static function get_attempt_stat_joins_params($cmid, $quantifiers = null
'cmid5' => $cmid,
'cmid6' => $cmid,
'cmid7' => $cmid,
'contextid1' => $contextid,
'contextid2' => $contextid,
'contextid3' => $contextid,
'contextid4' => $contextid,
'contextid5' => $contextid,
'status1' => question_version_status::QUESTION_STATUS_HIDDEN,
'status2' => question_version_status::QUESTION_STATUS_HIDDEN,
'status3' => question_version_status::QUESTION_STATUS_HIDDEN,
Expand Down Expand Up @@ -433,6 +444,7 @@ public static function get_question_stats($cmid, $groupid) {
JOIN {question_references} qr ON qr.itemid = sqq.id
AND qr.component = 'mod_studentquiz'
AND qr.questionarea = 'studentquiz_question'
AND qr.usingcontextid = :contextid1
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid AND qv.version = (
SELECT MAX(version)
Expand All @@ -449,6 +461,7 @@ public static function get_question_stats($cmid, $groupid) {
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
AND qr.component = 'mod_studentquiz'
AND qr.questionarea = 'studentquiz_question'
AND qr.usingcontextid = :contextid2
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid AND qv.version = (
SELECT MAX(version)
FROM {question_versions}
Expand All @@ -464,9 +477,12 @@ public static function get_question_stats($cmid, $groupid) {
'q.parent = 0',
'sq.coursemodule = :cmid1'
];
$contextid = \context_module::instance($cmid)->id;
$params = [
'cmid1' => $cmid,
'cmid2' => $cmid,
'contextid1' => $contextid,
'contextid2' => $contextid,
'status1' => question_version_status::QUESTION_STATUS_HIDDEN,
'status2' => question_version_status::QUESTION_STATUS_HIDDEN,
];
Expand Down

0 comments on commit 9c11882

Please sign in to comment.