From fca2d3d7542fa6b9d15c0433d7b2bb33add29843 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 12 May 2012 13:19:22 +0200 Subject: [PATCH] MDL-32659 potential navigation course loading improvements The idea is to not use inequalities in SQL queries for loading of courses in navigation because ti seems some SQL query optimiser have problems with that. --- lib/navigationlib.php | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 707ea920e05..5f44996d549 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1577,21 +1577,29 @@ class global_navigation extends navigation_node { // First up fetch all of the courses in categories where we know that we are going to // need the majority of courses. list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); - list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses) + array($SITE->id), SQL_PARAMS_NAMED, 'lcourse', false); list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory'); $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect FROM {course} c $ccjoin - WHERE c.category {$categoryids} AND - c.id {$courseids} + WHERE c.category {$categoryids} ORDER BY c.sortorder ASC"; - $coursesrs = $DB->get_recordset_sql($sql, $courseparams + $categoryparams); + $coursesrs = $DB->get_recordset_sql($sql, $categoryparams); foreach ($coursesrs as $course) { + if ($course->id == $SITE->id) { + // This should not be necessary, frontpage is not in any category. + continue; + } + if (array_key_exists($course->id, $this->addedcourses)) { + // It is probably better to not include the already loaded courses + // directly in SQL because inequalities may confuse query optimisers + // and may interfere with query caching. + continue; + } if (!$this->can_add_more_courses_to_category($course->category)) { continue; } context_instance_preload($course); - if ($course->id != $SITE->id && !$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { + if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { continue; } $coursenodes[$course->id] = $this->add_course($course); @@ -1604,21 +1612,30 @@ class global_navigation extends navigation_node { // proportion of the courses. foreach ($partfetch as $categoryid) { list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); - list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses) + array($SITE->id), SQL_PARAMS_NAMED, 'lcourse', false); $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect FROM {course} c $ccjoin - WHERE c.category = :categoryid AND - c.id {$courseids} + WHERE c.category = :categoryid ORDER BY c.sortorder ASC"; - $courseparams['categoryid'] = $categoryid; + $courseparams = array('categoryid' => $categoryid); $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5); foreach ($coursesrs as $course) { + if ($course->id == $SITE->id) { + // This should not be necessary, frontpage is not in any category. + continue; + } + if (array_key_exists($course->id, $this->addedcourses)) { + // It is probably better to not include the already loaded courses + // directly in SQL because inequalities may confuse query optimisers + // and may interfere with query caching. + // This also helps to respect expected $limit on repeated executions. + continue; + } if (!$this->can_add_more_courses_to_category($course->category)) { break; } context_instance_preload($course); - if ($course->id != $SITE->id && !$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { + if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { continue; } $coursenodes[$course->id] = $this->add_course($course); @@ -1637,8 +1654,12 @@ class global_navigation extends navigation_node { ORDER BY c.sortorder ASC"; $coursesrs = $DB->get_recordset_sql($sql, $courseparams); foreach ($coursesrs as $course) { + if ($course->id == $SITE->id) { + // frotpage is not wanted here + continue; + } context_instance_preload($course); - if ($course->id != $SITE->id && !$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { + if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { continue; } $coursenodes[$course->id] = $this->add_course($course);