有个场景比如是商品搜索的,按照价格排序,感觉除了limit 1000,10这种写法没有啥优化的办法了啊
比如我们日常优化分页是取到最后一条的数据id,进行大于或者小于取数据,比如:
sql语句:
select id, name from user where id > 10 limit 10;
但上面这种场景要求至少id是连续的比如 查询出来的结果数据商品id是
5 4 3 2 1
这种情况,用 > lastId的方式,很适合。但是如果按照价格排序之后的商品id是:
4 5 2 3 1
那么再用 > lastId这种方式,就不行了。所以 求教各位db大神,这种情况该如何优化?
select ... from
tab a
inner join
(select lastid from tab where ... order by date limit 10000,10) b
on a.lastid=b.lastid;
派生表b建个覆盖索引,试试效率;
lastid如果不是主键,也要建个索引。