关于大表查询最后几页过慢的原因

mysql数据量很大使用limit查询最后几页很慢,你知道原因么?

我们不妨先来看看几条查询语句与结果。

select * from test_user where id>9900000 limit 0,10;
select * from test_user limit 9900000,10;
select * from test_user where id in (select b.id from (select id from test_user where id>9900000 limit 0,10) as b); //子查询

[SQL]select * from test_user where id>9900000 limit 0,10;
受影响的行: 0
时间: 0.019s


[SQL]
select * from test_user limit 9900000,10;
受影响的行: 0
时间: 1.147s


[SQL]
select * from test_user where id in (select b.id from (select id from test_user where id>9900000 limit 0,10) as b);
受影响的行: 0
时间: 0.001s

其实,原因就在于:

limit10000,20的意思扫描满足条件的10020行,扔掉前面的10000行,返回最后的20行,问题就在这里。

解决方案:

  1. 过一半数据采用倒序查询
  2. 运用子查询

关于大表查询最后几页过慢的原因
https://blog.puresai.com/2018/05/31/157/
作者
puresai
许可协议