简体   繁体   中英

Possible to make this SQL query faster?

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.

What is your TAG table look like? Does it contain only ID and TAG fields? I would create a an unique index on TAG(TAG, ID) so the inner most query just searches an index.

What about POST_TAGS table? Is it just a combination of TAG_ID and IMAGE_ID? Again, I would create an unique index on POST_TAGS(IMAGE_ID, TAG_ID) .

Just to note that order of fields in an index is important, and an index on POST_TAGS(TAG_ID, IMAGE_ID) differs much with an index on POST_TAGS(IMAGE_ID, TAG_ID) using in parsing plans.

And in the POSTS table having an unique index on POSTS(ID, POSTS, RATEACCOUNT) can help, (I know it is an redundant index, and make extra cost on INSERT, UPDATE and DELETE, but it will help you in this query).

By the way, using a JOIN in the inner query may give you performance gain, just check it.

Managed to get it working using a JOIN as someone suggested.

SELECT * FROM posts
LEFT JOIN post_tags ON post_tags.image_id = posts.id
JOIN tags ON post_tags.tag_id = tags.id
WHERE tags.tag = _utf8 '$tag' collate utf8_bin
  AND posts.exists = 'n'
ORDER BY posts.ratecount DESC
LIMIT 0,100

Reduced time taken from 22s > 0.26s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM