MySQL分页性能问题,Limit 性能问题求解决!

WBOY
Release: 2016-06-06 20:48:48
Original
1289 people have browsed it

众所周知,Mysql分页就要用到Limit进行分页,数据量/分页数小的时候Limit性能是可想而知的。 如:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 100,100;
</code>
Copy after login
Copy after login

上述SQL语句性能没有任何问题。

但是,如果当offset便宜了过大的情况就会出现性能瓶颈。 如:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 159000,100;
</code>
Copy after login
Copy after login

若要解决Limit offset过大的时候,可以采用子查询的方法进行分页。 如:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts ORDER BY pid ASC LIMIT 159000,100;
</code>
Copy after login
Copy after login

等价于:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts WHERE pid>=(SELECT pid FROM posts LIMIT 159000,1)ORDER BY pid ASC LIMIT 100;
</code>
Copy after login
Copy after login

此时的性能是纯粹LIMIT 159000,100 的几倍至高。

但是问题又来了,子查询又必须是数据连续,所以无法加入WHERE查询条件,如:

<code class="lang-mysql">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;
</code>
Copy after login
Copy after login

此时的结果并非我想要的结果。

所以求高性能解决LIMIT分页的方法。 在线等。

============================ PS: pid 为主键

回复内容:

众所周知,Mysql分页就要用到Limit进行分页,数据量/分页数小的时候Limit性能是可想而知的。 如:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 100,100;
</code>
Copy after login
Copy after login

上述SQL语句性能没有任何问题。

但是,如果当offset便宜了过大的情况就会出现性能瓶颈。 如:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 159000,100;
</code>
Copy after login
Copy after login

若要解决Limit offset过大的时候,可以采用子查询的方法进行分页。 如:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts ORDER BY pid ASC LIMIT 159000,100;
</code>
Copy after login
Copy after login

等价于:

<code class="lang-mysql">SELECT pid,author,hash,dateline FROM posts WHERE pid>=(SELECT pid FROM posts LIMIT 159000,1)ORDER BY pid ASC LIMIT 100;
</code>
Copy after login
Copy after login

此时的性能是纯粹LIMIT 159000,100 的几倍至高。

但是问题又来了,子查询又必须是数据连续,所以无法加入WHERE查询条件,如:

<code class="lang-mysql">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;
</code>
Copy after login
Copy after login

此时的结果并非我想要的结果。

所以求高性能解决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

<code>    ALTER TABLE `posts `  ADD INDEX `tid_pid` USING BTREE (`tid`,`pid`) comment '';
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!