MDL-51514 gradebook: Optimize set_usedinaggregation query for MySQL

This commit is contained in:
Sam Chaffee 2015-10-02 11:28:15 -06:00
parent 35d3e8b00b
commit 84ab39cf77

View File

@ -793,15 +793,28 @@ class grade_category extends grade_object {
// Reset aggregation to unknown and 0 for all grade items for this user and category.
$params = array('categoryid' => $this->id, 'userid' => $userid);
$itemssql = "SELECT id
FROM {grade_items}
WHERE categoryid = :categoryid";
$sql = "UPDATE {grade_grades}
SET aggregationstatus = 'unknown',
aggregationweight = 0
WHERE userid = :userid
AND itemid IN ($itemssql)";
switch ($DB->get_dbfamily()) {
case 'mysql':
// Optimize the query for MySQL by using a join rather than a sub-query.
$sql = "UPDATE {grade_grades} g
JOIN {grade_items} gi ON (g.itemid = gi.id)
SET g.aggregationstatus = 'unknown',
g.aggregationweight = 0
WHERE g.userid = :userid
AND gi.categoryid = :categoryid";
break;
default:
$itemssql = "SELECT id
FROM {grade_items}
WHERE categoryid = :categoryid";
$sql = "UPDATE {grade_grades}
SET aggregationstatus = 'unknown',
aggregationweight = 0
WHERE userid = :userid
AND itemid IN ($itemssql)";
}
$DB->execute($sql, $params);