From a2aac5147d785cc8fd10e03c4be00398813cc8b8 Mon Sep 17 00:00:00 2001 From: Neill Magill Date: Fri, 3 Oct 2014 10:27:06 +0100 Subject: [PATCH] MDL-46182 Inefficient query during Moodle upgrade on course_section table. This part of the code is changing an index on the course_sections table to be unique, for this to happen in the upgrade script the index must be deleted and then the new version added. Before this change the following was being done: * The index is removed * A query to delete records that were not unique on this index is performed * The unique version of the index is added After this change the following happens: * A query to delete records that were not unique on this index is performed * The index is removed * The unique version of the index is added When the original index is present the query uses it, which greatly improves the execution plan. Before the change an upgrade on a site with around 387967 course_section records the upgrade would be stuck on the delete query for many hours, after the change the time can be measured in minutes. --- lib/db/upgrade.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 2ae31fab754..a3880a711d2 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -358,16 +358,6 @@ function xmldb_main_upgrade($oldversion) { if ($oldversion < 2012042300.00) { // This change makes the course_section index unique. - // xmldb does not allow changing index uniqueness - instead we must drop - // index then add it again - $table = new xmldb_table('course_sections'); - $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section')); - - // Conditionally launch drop index course_section - if ($dbman->index_exists($table, $index)) { - $dbman->drop_index($table, $index); - } - // Look for any duplicate course_sections entries. There should not be // any but on some busy systems we found a few, maybe due to previous // bugs. @@ -389,6 +379,19 @@ function xmldb_main_upgrade($oldversion) { $rs->close(); $transaction->allow_commit(); + // XMLDB does not allow changing index uniqueness - instead we must drop + // index then add it again. + // MDL-46182: The query to make the index unique uses the index, + // so the removal of the non-unique version needs to happen after any + // data changes have been made. + $table = new xmldb_table('course_sections'); + $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section')); + + // Conditionally launch drop index course_section. + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } + // Define index course_section (unique) to be added to course_sections $index = new xmldb_index('course_section', XMLDB_INDEX_UNIQUE, array('course', 'section')); -- 2.39.2