From aceb84ad7651f3b04788acf70fe72f29f6ba1c68 Mon Sep 17 00:00:00 2001 From: Tomek Muras Date: Wed, 6 Sep 2017 16:59:12 +0200 Subject: [PATCH] MDL-60043 accesslib: improve query performance by removing ORDER BY then add back the minimal necessary sorting in PHP. That part was added by Tim Hunt. --- lib/accesslib.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index bd400e4e351..856a80924b9 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -319,8 +319,7 @@ function get_role_definitions_uncached(array $roleids) { $sql = "SELECT ctx.path, rc.roleid, rc.capability, rc.permission FROM {role_capabilities} rc JOIN {context} ctx ON rc.contextid = ctx.id - WHERE rc.roleid $sql - ORDER BY ctx.path, rc.roleid, rc.capability"; + WHERE rc.roleid $sql"; $rs = $DB->get_recordset_sql($sql, $params); foreach ($rs as $rd) { @@ -334,6 +333,15 @@ function get_role_definitions_uncached(array $roleids) { } $rs->close(); + + // Sometimes (e.g. get_user_capability_course_helper::get_capability_info_at_each_context) + // we process role definitinons in a way that requires we see parent contexts + // before child contexts. This sort ensures that works (and is faster than + // sorting in the SQL query). + foreach ($rdefs as $roleid => $rdef) { + ksort($rdefs[$roleid]); + } + return $rdefs; }