I have two queries to get related tags from a mysql database, one works, one does not, my question is: “why?”
Problem:
When executing the first query, the mysql server gets 100% cpu usage, and has to be restarted to function again.
Query 1 (does not work):
SELECT tags.*, COUNT(ct.company_id) AS count
FROM company2tag ct, tags
WHERE ct.company_id IN (
SELECT ct.company_id FROM company2tag ct
WHERE ct.tag_id = 18
GROUP BY ct.company_id
HAVING COUNT(ct.company_id) = 1
)
AND tags.id != 18
AND tags.id = ct.tag_id
GROUP BY ct.tag_id
ORDER BY count DESC
LIMIT 5;
Query 2 (works):
SELECT tags.*, COUNT(ct.company_id) AS count
FROM company2tag ct, tags
WHERE ct.company_id IN (5864, 5870, 6140, 6221, 6268)
AND tags.id != 18
AND tags.id = ct.tag_id
GROUP BY ct.tag_id
ORDER BY count DESC
LIMIT 5;
To my understanding the two queries above do completely the same, the only difference is that the first query retrieves its “company_id’s” via a subselect.
How can this happen?
First of all, you may be experiencing problems from the first query because you have two tables aliased to ct… one in the outer query, one in the sub-query.
Secondly, you can rewrite the IN as a JOIN:
Note that I haven’t actually tested this.