From 07dffdf5f50beb9bf3d5e958644114b429aa3d8c Mon Sep 17 00:00:00 2001 From: Brian King Date: Fri, 23 Aug 2013 13:49:03 +0200 Subject: [PATCH] MDL-41403 Backup: reduce memory usage when searching for courses / categories Also makes a few existing comments in the changed code area adhere to coding standards. --- backup/util/ui/restore_ui_components.php | 28 +++++++++++------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/backup/util/ui/restore_ui_components.php b/backup/util/ui/restore_ui_components.php index 6f655330796..f892b8cf108 100644 --- a/backup/util/ui/restore_ui_components.php +++ b/backup/util/ui/restore_ui_components.php @@ -174,22 +174,20 @@ abstract class restore_search_base implements renderable { $this->totalcount = 0; $contextlevel = $this->get_itemcontextlevel(); list($sql, $params) = $this->get_searchsql(); - $blocksz = 5000; - $offs = 0; - // Get total number, to avoid some incorrect iterations + // Get total number, to avoid some incorrect iterations. $countsql = preg_replace('/ORDER BY.*/', '', $sql); $totalcourses = $DB->count_records_sql("SELECT COUNT(*) FROM ($countsql) sel", $params); - // User to be checked is always the same (usually null, get it form first element) - $firstcap = reset($this->requiredcapabilities); - $userid = isset($firstcap['user']) ? $firstcap['user'] : null; - // Extract caps to check, this saves us a bunch of iterations - $requiredcaps = array(); - foreach ($this->requiredcapabilities as $cap) { - $requiredcaps[] = $cap['capability']; - } - // Iterate while we have records and haven't reached $this->maxresults. - while ($totalcourses > $offs and $this->totalcount < $this->maxresults) { - $resultset = $DB->get_records_sql($sql, $params, $offs, $blocksz); + if ($totalcourses > 0) { + // User to be checked is always the same (usually null, get it from first element). + $firstcap = reset($this->requiredcapabilities); + $userid = isset($firstcap['user']) ? $firstcap['user'] : null; + // Extract caps to check, this saves us a bunch of iterations. + $requiredcaps = array(); + foreach ($this->requiredcapabilities as $cap) { + $requiredcaps[] = $cap['capability']; + } + // Iterate while we have records and haven't reached $this->maxresults. + $resultset = $DB->get_recordset_sql($sql, $params); foreach ($resultset as $result) { context_helper::preload_from_record($result); $classname = context_helper::get_class_for_level($contextlevel); @@ -208,7 +206,7 @@ abstract class restore_search_base implements renderable { $this->totalcount++; $this->results[$result->id] = $result; } - $offs += $blocksz; + $resultset->close(); } return $this->totalcount; -- 2.43.0