Just curious if it’s possible to make this query any faster? or if there is any other similar queries that would work better?
SELECT id,source FROM posts
WHERE id = ANY(SELECT image_id FROM `post_tags` WHERE tag_id = (SELECT id FROM `tags` WHERE tag = _utf8 '$TAG' collate utf8_bin))
AND posts.exists = 'n'
ORDER BY posts.ratecount DESC
LIMIT 0,100
Without using the:
AND posts.exists = 'n'
ORDER BY posts.ratecount
DESC LIMIT 0,100
It speeds up to query to usable levels, but somewhat need this for what I’m doing.
- Tags table has unique index for both ‘tag’ and ‘id’.
- Tags has 83K rows.
- Post_tags has unique index for ‘image_id’, ‘tag_id’. Also normal index for each.
- Post_tags has 471K rows.
- Posts has unique index for ‘id’. Also normal index for ‘exists’ and ‘ratecount’.
- Posts table has about 1.1M rows.
Managed to get it working using a JOIN as someone suggested.
Reduced time taken from 22s > 0.26s.