Hi, i have a question about basic mysql database optimisation. I have 3 tables, Articles, Tags and Taggings (which is a join table).
Articles Taggings Tags
id id id
name article_id name
tag_id
I am retrieving the articles that exactly match the tags specified, with the following query
SELECT *, COUNT(*) AS c
FROM articles AS a
JOIN taggings AS tng ON a.id = tng.article_id
JOIN tags AS t ON t.id = tng.tag_id
WHERE t.name IN ("Red","Green")
GROUP BY a.id
HAVING c = 2
This query is slow, so I did an EXPLAIN, and got the following results:
Now, I don't really understand what I am doing here, but i believe that "type: ALL" is not good, so thought i would add indexes(BTREE) to both article_id and tag_id in the taggings table, and run the query again. Well that didn't look any better to my uneducated eye, same number of rows as previous one, and the type is still ALL in two of the cases.
So could someone tell me where I am going wrong please? will indexes not help me with this problem?
My Tag table will remain relatively small, so I thought the query should scan the Tag table for the tags I have specified, and then (through the indexes) be able to instantly retrieve the associated properties, and it should all be very quick, obviously something wrong in my thinking.
Thanks
[EDIT] - for Jay's comments
I added 10k articles, 30k taggings, and 6 tags, also added 2 indexs on tag.name and taggings.tag_id, the query still took a long time to run, 0.5-1 second, the EXPLAIN is below.