asc速度慢 mysql,MySQL ORDER BY DESC很快,但是ASC却很慢

I'm completely stumped on this one. For some reason when I sort this query by DESC it's super fast, but if sorted by ASC it's extremely slow.

This takes about 150 milliseconds:

SELECT posts.id

FROM posts USE INDEX (published)

WHERE posts.feed_id IN ( 4953,622,1,1852,4952,76,623,624,10 )

ORDER BY posts.published DESC

LIMIT 0, 50;

This takes about 32 seconds:

SELECT posts.id

FROM posts USE INDEX (published)

WHERE posts.feed_id IN ( 4953,622,1,1852,4952,76,623,624,10 )

ORDER BY posts.published ASC

LIMIT 0, 50;

The EXPLAIN is the same for both queries.

id select_type table type possible_keys key key_len ref rows Extra

1 SIMPLE posts index NULL published 5 NULL 50 Using where

I've tracked it down to "USE INDEX (published)". If I take that out it's the same performance both ways. But the EXPLAIN shows the query is less efficient overall.

id select_type table type possible_keys key key_len ref rows Extra

1 SIMPLE posts range feed_id feed_id 4 \N 759 Using where; Using filesort

And here's the table.

CREATE TABLE `posts` (

`id` int(20) NOT NULL AUTO_INCREMENT,

`feed_id` int(11) NOT NULL,

`post_url` varchar(255) NOT NULL,

`title` varchar(255) NOT NULL,

`content` blob,

`author` varchar(255) DEFAULT NULL,

`published` int(12) DEFAULT NULL,

`updated` datetime NOT NULL,

`created` datetime NOT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `post_url` (`post_url`,`feed_id`),

KEY `feed_id` (`feed_id`),

KEY `published` (`published`)

) ENGINE=InnoDB AUTO_INCREMENT=196530 DEFAULT CHARSET=latin1;

Is there a fix for this? Thanks!

解决方案

Your index is sorted desc so when you ask for ascending it needs to do a lot more work to bring it back in that order

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值