From 42574d0900abf6ac3745b61aa9817db3792a626d Mon Sep 17 00:00:00 2001 From: Russell Smith Date: Tue, 9 Jul 2013 20:04:09 +1000 Subject: [PATCH] MDL-40584 backup: Query db once per category in precheck The cache is function local and testing against a large database indicates 10k questions is a large category. Restore already uses MEMORY_EXTRA and that will have enough space for the couple of megabtyes a local sql hash will introduce. --- backup/util/dbops/restore_dbops.class.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 7feafe8cf61..230fb454bfc 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -614,13 +614,20 @@ abstract class restore_dbops { } else { self::set_backup_ids_record($restoreid, 'question_category', $category->id, $matchcat->id, $targetcontext->id); $questions = self::restore_get_questions($restoreid, $category->id); + + // Collect all the questions for this category into memory so we only talk to the DB once. + $questioncache = $DB->get_records_sql_menu("SELECT ".$DB->sql_concat('stamp', "' '", 'version').", id + FROM {question} + WHERE category = ?", array($matchcat->id)); + foreach ($questions as $question) { - $matchq = $DB->get_record('question', array( - 'category' => $matchcat->id, - 'stamp' => $question->stamp, - 'version' => $question->version)); + if (isset($questioncache[$question->stamp." ".$question->version])) { + $matchqid = $questioncache[$question->stamp." ".$question->version]; + } else { + $matchqid = false; + } // 5a) No match, check if user can add q - if (!$matchq) { + if (!$matchqid) { // 6a) User can, mark the q to be created if ($canadd) { // Nothing to mark, newitemid means create @@ -645,7 +652,7 @@ abstract class restore_dbops { // 5b) Match, mark q to be mapped } else { - self::set_backup_ids_record($restoreid, 'question', $question->id, $matchq->id); + self::set_backup_ids_record($restoreid, 'question', $question->id, $matchqid); } } } -- 2.43.0