select pid,author,hash,dateline from posts where pid='123456' order by pid asc limit 100,100;
上述sql语句性能没有任何问题。
但是,如果当offset便宜了过大的情况就会出现性能瓶颈。如:
select pid,author,hash,dateline from posts where pid='123456' order by pid asc limit 159000,100;
若要解决limit offset过大的时候,可以采用子查询的方法进行分页。如:
select pid,author,hash,dateline from posts order by pid asc limit 159000,100;
等价于:
select pid,author,hash,dateline from posts where pid>=(select pid from posts limit 159000,1)order by pid asc limit 100;
此时的性能是纯粹limit 159000,100 的几倍至高。
但是问题又来了,子查询又必须是数据连续,所以无法加入where查询条件,如:
select tid,pid,author,hash,dateline from posts where tid=10 and pid>=(select pid from posts limit 159000,1)order by pid asc limit 100;
此时的结果并非我想要的结果。
所以求高性能解决limit分页的方法。在线等。
============================ps: pid 为主键
回复内容: 众所周知,mysql分页就要用到limit进行分页,数据量/分页数小的时候limit性能是可想而知的。如:
select pid,author,hash,dateline from posts where pid='123456' order by pid asc limit 100,100;
上述sql语句性能没有任何问题。
但是,如果当offset便宜了过大的情况就会出现性能瓶颈。如:
select pid,author,hash,dateline from posts where pid='123456' order by pid asc limit 159000,100;
若要解决limit offset过大的时候,可以采用子查询的方法进行分页。如:
select pid,author,hash,dateline from posts order by pid asc limit 159000,100;
等价于:
select pid,author,hash,dateline from posts where pid>=(select pid from posts limit 159000,1)order by pid asc limit 100;
此时的性能是纯粹limit 159000,100 的几倍至高。
但是问题又来了,子查询又必须是数据连续,所以无法加入where查询条件,如:
select tid,pid,author,hash,dateline from posts where tid=10 and pid>=(select pid from posts limit 159000,1)order by pid asc limit 100;
此时的结果并非我想要的结果。
所以求高性能解决limit分页的方法。在线等。
============================ps: pid 为主键
这篇pdf可以解决你的问题
efficient pagination using mysql
你的pid是啥,不是主键吗?最后一条pid=10 and pid>=(select pid from posts limit 159000,1)有能同时成立吗
外面有的条件,子查询里面照样写就行了,和子查询的条件相同,order by相同,比如:select * from user where sex=2 and issingle=0 and id
alter table `posts ` add index `tid_pid` using btree (`tid`,`pid`) comment '';
