I’ve been working through a MySQL query and am wondering if there is a better way to do this (without correlated subqueries, etc…). The query in question is
SELECT
(SELECT COUNT(*) as num_visits FROM visit WHERE data_type='gallery'
and gallery.id=visit.object_id) as num_visits, gallery.id
FROM gallery
ORDER BY num_visits desc
The relavant parts of the table structure are
gallery
-------
id
visit
-----
id
data_type --> enum('gallery','etc')
object_id --> ties to various tables
Any help with this would be greatly appreciated.
But the better solution (from performance perspective) would be to have the amount of visits precalculated and stored right in
gallerytable (or in some other)