云数据库场景问题经典案例——分页优化

普通写法:

select * from t where sellerid=100 limit 10000020

普通limit M,N的翻页写法,往往在越往后翻页的过程中速度越慢,原因mysql会读取表中的前M+N条数据, M越大,性能就越差:

优化写法:

select t1.* from t t1, (select id from t sellerid=100 limit 10000020) t2 where t1.id=t2.id;

 优化后的翻页写法,先查询翻页中需要的N条数据的主键id,在根据主键id回表查询所需要的N条数据,此过 程中查询N条数据的主键ID在索引中完成

注意:需要在t表的sellerid字段上创建索引 create index ind_sellerid on t(sellerid);

案例如下:

原始语句:

select id, ... from buyer where sellerId = 765922982 and gmt_modified >= '1970-01-01 08:00:00' and gmt_modified <= '2013-06-05 17:11:31' limit 255000, 5000

优化后语句:

select t2.* from (select id from buyer where sellerId = 765922982 and gmt_modified >= '1970-01-01 08:00:00' and gmt_modified <= '2013-06-05 17:11:31' limit 255000, 5000)t1, buyer t2 where t1.id=t2.id ;
原文地址:https://www.cnblogs.com/lwlxqlccc/p/8568564.html