- Affected version
- 2.1.2
Code:
SELECT COUNT(*)
FROM `xf_mg_media_item`
LEFT JOIN `xf_mg_album` AS `xf_mg_album_Album_1` ON (`xf_mg_album_Album_1`.`album_id` = `xf_mg_media_item`.`album_id`)
WHERE (IF(`xf_mg_media_item`.`album_id` > 0, `xf_mg_album_Album_1`.`album_state` = 'visible', 1=1))
AND ((`xf_mg_media_item`.`category_id` IN (X, Y, Z)) OR (`xf_mg_media_item`.`category_id` = 0 AND `xf_mg_album_Album_1`.`view_privacy` = 'public'))
AND ((`xf_mg_media_item`.`media_state` IN ('visible')))
This query is slow as it can't use any existing index in
xf_mg_media_item
and thus causes a full table scan.The following alternative yields the same results and seems a lot faster
Code:
SELECT
(
SELECT SUM(`media_count`)
FROM `xf_mg_category`
WHERE `category_id` IN (X, Y, Z)
)
+
(
SELECT SUM(`media_count`)
FROM `xf_mg_album`
WHERE `album_state` = 'visible'
AND `view_privacy` = 'public'
)