can someone explain why adding a group with a subquery makes this request long (30 seconds):
SELECT * FROM aggregate_songlist AS a INNER JOIN musical_works AS m ON a.musical_work_id = m.id WHERE m.genre='rock' AND m.id NOT IN (SELECT sources.musical_work_id FROM sources GROUP BY sources.musical_work_id HAVING COUNT(sources.musical_work_id) > 8)
If I delete the "group by" (and increasing the results of the subquery), it takes 0.07 seconds:
SELECT * FROM aggregate_songlist AS a INNER JOIN musical_works AS m ON a.musical_work_id = m.id WHERE m.genre='rock' AND m.id NOT IN (SELECT sources.musical_work_id FROM sources)
There are no external links in the subquery, so it should only be executed once, right? Doing this yourself:
SELECT sources.musical_work_id FROM sources GROUP BY sources.musical_work_id HAVING COUNT(sources.musical_work_id) > 8
takes only 0.01 seconds.
Any explanation? Any suggestions on how to change it?
source share