From 35a59dd032a82e369db75888af09e368fe6e221b Mon Sep 17 00:00:00 2001 From: Jonathan Champ Date: Thu, 23 Feb 2023 14:01:53 -0500 Subject: [PATCH] MDL-77391 question admin: further improve query peformance in qtypes.php --- admin/qtypes.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/admin/qtypes.php b/admin/qtypes.php index 84986ebd706..b4b366361f3 100644 --- a/admin/qtypes.php +++ b/admin/qtypes.php @@ -43,21 +43,17 @@ $qtypes = question_bank::get_all_qtypes(); $pluginmanager = core_plugin_manager::instance(); // Get some data we will need - question counts and which types are needed. +// The second JOIN on question_versions (qv2) is to get the latest version of each question.  +// (Using this sort of JOIN is a known trick for doing this in the fastest possible way.) $counts = $DB->get_records_sql(" SELECT q.qtype, COUNT(qv.id) AS numquestions, SUM(CASE WHEN qv.status = :hiddenstatus THEN 1 ELSE 0 END) AS numhidden, SUM(CASE WHEN qv.status = :draftstatus THEN 1 ELSE 0 END) AS numdraft - - FROM {question_bank_entries} qbe - JOIN {question_versions} qv ON qv.questionbankentryid = qbe.id - AND qv.version = ( - SELECT MAX(version) - FROM {question_versions} - WHERE questionbankentryid = qbe.id - ) - JOIN {question} q ON q.id = qv.questionid - + FROM {question} q + JOIN {question_versions} qv ON q.id = qv.questionid + LEFT JOIN {question_versions} qv2 ON qv.questionbankentryid = qv2.questionbankentryid AND qv.version < qv2.version + WHERE qv2.questionbankentryid IS NULL GROUP BY q.qtype ", [ 'hiddenstatus' => question_version_status::QUESTION_STATUS_HIDDEN,