I currently have the current MySQL query taking up to 10 seconds to run in my application:
SELECT tagid, tag FROM tags WHERE tagid IN
(SELECT DISTINCT tagid FROM news_tags WHERE newsid IN
(SELECT newsid FROM news_tags WHERE tagid IN (16,32)
GROUP BY newsid HAVING COUNT(newsid)>=2))
AND tagid NOT IN (16,32) ORDER BY level, tagid
The tables used are:
- table
news_tags, with columnsnewsid,tagid - table
tags, with columnstagid,tag,level
The purpose of the query is to find “news” items which have been tagged with tags with tagid 16 and 32, then find other tags these news items have also been tagged with, for the purposes of allowing a user to further narrow down the “news” items with more specific tag combinations. The ultimate goal is to grab the remaining relevant tag and tagid columns from the tags table.
I have tried different attempts at an equivalent JOIN but have failed to select all remaining tagids on the news items which have the provided tags attached to them.
Here is my EXPLAIN SQL results, in case they point to another cause of slowness which I’m missing:
id|select_type |table |type |possible_keys|key |key_len|ref |rows|Extra 1|PRIMARY |tags |range |PRIMARY |PRIMARY| 4|NULL| 55|Using where; Using filesort 2|DEPENDENT SUBQUERY|news_tags|index_subquery|tagid |tagid | 4|func| 26|Using index; Using where 3|DEPENDENT SUBQUERY|news_tags|index |tagid |PRIMARY| 8|NULL| 11|Using where; Using index
Just to clarify the problem: I wanted remaining tags for news items tagged with BOTH tags 16 and 32, not either 16 or 32. Sorry for any confusion.
1 Answer