So essentially I have two tables, containing URLS and TAGS, with a has-and-belongs-to-many relationship between the two via a joins tables TAGS_URLS.
A simple query to find URL's by tags would be:
SELECT urls.id FROM urls
INNER JOIN tags_urls ON urls.id=tags_urls.url_id
INNER JOIN tags ON tags_urls.tag_id=tags.id
WHERE tags.tag IN ("sample","tag","list");
However, I'm trying to recover an intersection of all URL's that contain all of a set of tags. I.e., only URL's that contain the tag "sample" AND "tag" AND "list".
I have a working query, but I cannot get the query to execute in less than 30 seconds.
SELECT a.id
FROM
(SELECT DISTINCT urls.id FROM urls
INNER JOIN tags_urls ON tags_urls.url_id=urls.id INNER JOIN tags ON tags.id=tags_urls.tag_id
WHERE tags.tag = 'sample') a
JOIN
(SELECT DISTINCT urls.id FROM urls
INNER JOIN tags_urls ON tags_urls.url_id=urls.id INNER JOIN tags ON tags.id=tags_urls.tag_id
WHERE tags.tag = 'list') b
ON a.id = b.id;
The result set is correct, but the performance is horrific.
I do also currently have the data duplicated in a Redis database as a list of URL id's stored in tag sets so I can do something like this and get a result set VERY quickly.
SINTER "tag-sample" "tag-list"
Would it be possible, with reasonable effort, to bring the MySQL performance for this task up to the Redis levels with SINTER?