Skip to content

Commit

Permalink
Merge pull request #597 from catalyst/MOODLE_402_STABLE_issue596_gen_…
Browse files Browse the repository at this point in the history
…report_sql_tweaks

Issue #596: SQL change with some performance / resource improvements
  • Loading branch information
michaelkotlyar authored Feb 28, 2024
2 parents 789d092 + ddaef15 commit bb158f9
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions classes/local/report/location_report_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,52 @@ public function build_report($reportid) {
$filedircount = 0;
$filedirsum = 0;
foreach ($locations as $location) {
$localsql = '';
$sql =
'WITH
cte_objects AS (
SELECT o.contenthash, o.location
FROM {tool_objectfs_objects} o
WHERE o.location = ? ),
cte_obj_files AS (
SELECT f.contenthash, MAX(f.filesize) AS filesize
FROM {files} f
INNER JOIN cte_objects co ON f.contenthash = co.contenthash
WHERE filesize > 0
GROUP BY f.contenthash, f.filesize)
SELECT COALESCE(COUNT(cof.contenthash),0) AS objectcount,
COALESCE(SUM(cof.filesize),0) AS objectsum
FROM cte_obj_files cof';

if ($location == OBJECT_LOCATION_LOCAL) {
$localsql = ' or o.location IS NULL';
$sql =
'WITH
cte_objects AS (
SELECT o.contenthash, o.location
FROM {tool_objectfs_objects} o ),
cte_obj_files AS (
SELECT f.contenthash, MAX(f.filesize) AS filesize
FROM {files} f
LEFT JOIN cte_objects co ON f.contenthash = co.contenthash
WHERE filesize > 0 AND ( co.location = ? OR co.location IS NULL )
GROUP BY f.contenthash, f.filesize)
SELECT COALESCE(COUNT(cof.contenthash),0) AS objectcount,
COALESCE(SUM(cof.filesize),0) AS objectsum
FROM cte_obj_files cof';
}

$sql = 'SELECT COALESCE(count(sub.contenthash) ,0) AS objectcount,
COALESCE(SUM(sub.filesize) ,0) AS objectsum
FROM (SELECT f.contenthash, MAX(f.filesize) AS filesize
FROM {files} f
LEFT JOIN {tool_objectfs_objects} o on f.contenthash = o.contenthash
GROUP BY f.contenthash, f.filesize, o.location
HAVING o.location = ?' . $localsql .') AS sub
WHERE sub.filesize > 0';

if ($location !== OBJECT_LOCATION_ORPHANED) {
// Process the query normally.
$result = $DB->get_record_sql($sql, array($location));
} else if ($location === OBJECT_LOCATION_ORPHANED) {
// Start the query from objectfs, for ORPHANED objects, they are not located in the files table.
$sql = 'SELECT COALESCE(count(sub.contenthash) ,0) AS objectcount
FROM (SELECT o.contenthash
FROM {tool_objectfs_objects} o
LEFT JOIN {files} f on f.contenthash = o.contenthash
GROUP BY o.contenthash, f.filesize, o.location
HAVING o.location = ?' . $localsql .') AS sub';
$sql =
'WITH
cte_objects AS (
SELECT o.contenthash
FROM {tool_objectfs_objects} o
WHERE o.location = ?)
SELECT COALESCE(COUNT(co.contenthash),0) AS objectcount
FROM cte_objects co';
$result = $DB->get_record_sql($sql, array($location));
$result->objectsum = 0;
}
Expand Down

0 comments on commit bb158f9

Please sign in to comment.