@@ -50,31 +50,52 @@ public function build_report($reportid) {
50
50
$ filedircount = 0 ;
51
51
$ filedirsum = 0 ;
52
52
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
+
54
69
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 ' ;
56
84
}
57
85
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
-
67
86
if ($ location !== OBJECT_LOCATION_ORPHANED ) {
68
87
// Process the query normally.
69
88
$ result = $ DB ->get_record_sql ($ sql , array ($ location ));
70
89
} else if ($ location === OBJECT_LOCATION_ORPHANED ) {
71
90
// 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 ' ;
78
99
$ result = $ DB ->get_record_sql ($ sql , array ($ location ));
79
100
$ result ->objectsum = 0 ;
80
101
}
0 commit comments