Skip to content

Commit ddaef15

Browse files
committed
Issue #596: SQL change with some performance / resource improvements
1 parent 789d092 commit ddaef15

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

classes/local/report/location_report_builder.php

+38-17
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,52 @@ public function build_report($reportid) {
5050
$filedircount = 0;
5151
$filedirsum = 0;
5252
foreach ($locations as $location) {
53-
$localsql = '';
53+
$sql =
54+
'WITH
55+
cte_objects AS (
56+
SELECT o.contenthash, o.location
57+
FROM {tool_objectfs_objects} o
58+
WHERE o.location = ? ),
59+
cte_obj_files AS (
60+
SELECT f.contenthash, MAX(f.filesize) AS filesize
61+
FROM {files} f
62+
INNER JOIN cte_objects co ON f.contenthash = co.contenthash
63+
WHERE filesize > 0
64+
GROUP BY f.contenthash, f.filesize)
65+
SELECT COALESCE(COUNT(cof.contenthash),0) AS objectcount,
66+
COALESCE(SUM(cof.filesize),0) AS objectsum
67+
FROM cte_obj_files cof';
68+
5469
if ($location == OBJECT_LOCATION_LOCAL) {
55-
$localsql = ' or o.location IS NULL';
70+
$sql =
71+
'WITH
72+
cte_objects AS (
73+
SELECT o.contenthash, o.location
74+
FROM {tool_objectfs_objects} o ),
75+
cte_obj_files AS (
76+
SELECT f.contenthash, MAX(f.filesize) AS filesize
77+
FROM {files} f
78+
LEFT JOIN cte_objects co ON f.contenthash = co.contenthash
79+
WHERE filesize > 0 AND ( co.location = ? OR co.location IS NULL )
80+
GROUP BY f.contenthash, f.filesize)
81+
SELECT COALESCE(COUNT(cof.contenthash),0) AS objectcount,
82+
COALESCE(SUM(cof.filesize),0) AS objectsum
83+
FROM cte_obj_files cof';
5684
}
5785

58-
$sql = 'SELECT COALESCE(count(sub.contenthash) ,0) AS objectcount,
59-
COALESCE(SUM(sub.filesize) ,0) AS objectsum
60-
FROM (SELECT f.contenthash, MAX(f.filesize) AS filesize
61-
FROM {files} f
62-
LEFT JOIN {tool_objectfs_objects} o on f.contenthash = o.contenthash
63-
GROUP BY f.contenthash, f.filesize, o.location
64-
HAVING o.location = ?' . $localsql .') AS sub
65-
WHERE sub.filesize > 0';
66-
6786
if ($location !== OBJECT_LOCATION_ORPHANED) {
6887
// Process the query normally.
6988
$result = $DB->get_record_sql($sql, array($location));
7089
} else if ($location === OBJECT_LOCATION_ORPHANED) {
7190
// Start the query from objectfs, for ORPHANED objects, they are not located in the files table.
72-
$sql = 'SELECT COALESCE(count(sub.contenthash) ,0) AS objectcount
73-
FROM (SELECT o.contenthash
74-
FROM {tool_objectfs_objects} o
75-
LEFT JOIN {files} f on f.contenthash = o.contenthash
76-
GROUP BY o.contenthash, f.filesize, o.location
77-
HAVING o.location = ?' . $localsql .') AS sub';
91+
$sql =
92+
'WITH
93+
cte_objects AS (
94+
SELECT o.contenthash
95+
FROM {tool_objectfs_objects} o
96+
WHERE o.location = ?)
97+
SELECT COALESCE(COUNT(co.contenthash),0) AS objectcount
98+
FROM cte_objects co';
7899
$result = $DB->get_record_sql($sql, array($location));
79100
$result->objectsum = 0;
80101
}

0 commit comments

Comments
 (0)