Mysql slow query: INNER JOIN + ORDER BY causes filesort

丶灬走出姿态 提交于 2019-11-29 10:01:59

You would need to denormalize a bit, and copy the posts.created_at field into the post_tags table (I called it post_created_at, you could name it how you want):

CREATE TABLE `posts_tags` (
  `id` int(11) NOT NULL auto_increment,
  `post_id` int(11) default NULL,
  `tag_id` int(11) default NULL,
  `post_created_at` datetime default NULL,
  `created_at` datetime default NULL,
  `updated_at` datetime default NULL,
  PRIMARY KEY  (`id`),
  KEY `index_posts_tags_on_post_id_and_tag_id` (`post_id`,`tag_id`)
) ENGINE=InnoDB;

and then add an index to posts_tags on

(tag_id, post_created_at)

That will allow the query to get all the posts for a tag, in the correct order, without filesort.

Try changing KEY index_posts_tags_on_post_id_and_tag_id (post_id,tag_id) to KEY index_posts_tags_tag_id (tag_id) and repost Explain.

What is the distribution of TagIDs withing Posts_Tags?

your key index_posts_on_created_at is sorted ascending but you want results sorted descending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!