Skip to content

Commit

Permalink
MDL-51514 gradebook: Optimize set_usedinaggregation query for MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
samchaffee authored and mdjnelson committed Oct 4, 2015
1 parent dc3850f commit f0d7264
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/grade/grade_category.php
Expand Up @@ -793,15 +793,28 @@ private function set_usedinaggregation($userid, $usedweights, $novalue, $dropped

// 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);

Expand Down

0 comments on commit f0d7264

Please sign in to comment.