用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一起读出来怎么办呢。难道要分两次读才行吗。
...全文
118010打赏收藏
mysql加了order by特别慢的问题
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一起读出来怎么办呢。难道要分两次读才行吗。
非常感谢版主大人的回复!
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所在行的数据吗。
确实没用到索引。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)
这是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)
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,不需要再去访问数据文件。