mysql加了order by特别慢的问题

cxluzi 2012-11-07 03:35:40
mysql info表,id 主键 自增长 数据20万左右


用order by id 特别慢
select id,title from info order by id desc limit 80100,50; |用时8秒

select id from info order by id desc limit 80100,50; |用时0.05秒

为什么差这么多呢? 能解释下吗

如果我想title一起读出来怎么办呢。难道要分两次读才行吗。


...全文
1180 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fxxyaya 2012-11-08
  • 打赏
  • 举报
回复 1
引用 7 楼 cxluzi 的回复:
非常感谢版主大人的回复! select id,title from info where status = 3 order by id desc limit 90000, 50; 这条语句,explain看是用到索引了。为什么执行SQL的时候还是很慢。也要10多秒! 去掉 order by id desc,要快点,0。7秒。还是慢 难道这种情况只能 先……
你要看到用到索引是status的索引而不是order by字段的索引,不一样,所以最后还是用到了filesort来排序,所以速度慢
cxluzi 2012-11-08
  • 打赏
  • 举报
回复
非常感谢版主大人的回复! select id,title from info where status = 3 order by id desc limit 90000, 50; 这条语句,explain看是用到索引了。为什么执行SQL的时候还是很慢。也要10多秒! 去掉 order by id desc,要快点,0。7秒。还是慢 难道这种情况只能 先查出ID组,然用得到的ID组去多次取ID所在行的数据吗。
wwwwb 2012-11-08
  • 打赏
  • 举报
回复
试试: select id,title from info a inner join (select id from info order by id desc limit 80100,50) b on a.id=b.id
cxluzi 2012-11-08
  • 打赏
  • 举报
回复
确实没用到索引。id是主键,order by 也不行吗。实在不解啊 另外如果加了where语句。再order by id就用到索引了。 这是为什么呢。请赐教!!! mysql> desc select id,title from destoon_sell info where status = 3 order by id desc limit 90000, 50; +----+-------------+-------+------+---------------+--------+---------+-------+--------+-----------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+--------+---------+-------+--------+-----------------------------+ | 1 | SIMPLE | info | ref | status | status | 1 | const | 198341 | Using where; Using filesort | +----+-------------+-------+------+---------------+--------+---------+-------+--------+-----------------------------+ 1 row in set (0.11 sec)
cxluzi 2012-11-08
  • 打赏
  • 举报
回复
这是explain结果: mysql> desc select id ,title from info order by id desc limit 90000, 50; +----+-------------+-------+------+---------------+------+---------+------+--------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+--------+----------------+ | 1 | SIMPLE | info | ALL | NULL | NULL | NULL | NULL | 227903 | Using filesort | +----+-------------+-------+------+---------------+------+---------+------+--------+----------------+ 1 row in set (0.00 sec)
fxxyaya 2012-11-08
  • 打赏
  • 举报
回复
给id和title建立一个联合索引试试
ACMAIN_CHM 2012-11-07
  • 打赏
  • 举报
回复
select id,title from info order by id desc limit 80100,50; |用时8秒 这个MYSQL需要在索引文件中获得 50个ID,然后到数据文件去取 title select id from info order by id desc limit 80100,50 而这个MYSQL仅通过访问索引文件就可以获得 ID,不需要再去访问数据文件。
bonlog 2012-11-07
  • 打赏
  • 举报
回复
最大的可能是哪些字段设置有误,延长检索时间。 id,是id的吗?
fxxyaya 2012-11-07
  • 打赏
  • 举报
回复
楼主用explain查看一下

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧