mirror of
https://github.com/moodle/moodle.git
synced 2025-07-18 21:01:27 +02:00
MDL-54582 accesslib: improve performance of load_course_context
The new query is logically equivalen, but much, much faster, at least on Postgred. (15ms, instead of 700ms, in one example I tried on the database for the OU's main Moodle site.)
This commit is contained in:
@@ -980,20 +980,17 @@ function load_course_context($userid, context_course $coursecontext, &$accessdat
|
|||||||
}
|
}
|
||||||
|
|
||||||
// now get overrides of interesting roles in all interesting contexts (this course + children + parents)
|
// now get overrides of interesting roles in all interesting contexts (this course + children + parents)
|
||||||
$params = array('c'=>$coursecontext->id);
|
$params = array('pathprefix' => $coursecontext->path . '/%');
|
||||||
list($parentsaself, $rparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'pc_');
|
list($parentsaself, $rparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'pc_');
|
||||||
$params = array_merge($params, $rparams);
|
$params = array_merge($params, $rparams);
|
||||||
list($roleids, $rparams) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'r_');
|
list($roleids, $rparams) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'r_');
|
||||||
$params = array_merge($params, $rparams);
|
$params = array_merge($params, $rparams);
|
||||||
|
|
||||||
$sql = "SELECT ctx.path, rc.roleid, rc.capability, rc.permission
|
$sql = "SELECT ctx.path, rc.roleid, rc.capability, rc.permission
|
||||||
FROM {role_capabilities} rc
|
FROM {context} ctx
|
||||||
JOIN {context} ctx
|
JOIN {role_capabilities} rc ON rc.contextid = ctx.id
|
||||||
ON (ctx.id = rc.contextid)
|
|
||||||
JOIN {context} cctx
|
|
||||||
ON (cctx.id = :c
|
|
||||||
AND (ctx.id $parentsaself OR ctx.path LIKE ".$DB->sql_concat('cctx.path',"'/%'")."))
|
|
||||||
WHERE rc.roleid $roleids
|
WHERE rc.roleid $roleids
|
||||||
|
AND (ctx.id $parentsaself OR ctx.path LIKE :pathprefix)
|
||||||
ORDER BY rc.capability"; // fixed capability order is necessary for rdef dedupe
|
ORDER BY rc.capability"; // fixed capability order is necessary for rdef dedupe
|
||||||
$rs = $DB->get_recordset_sql($sql, $params);
|
$rs = $DB->get_recordset_sql($sql, $params);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user