Use subqueries to improve MySQL paging Efficiency

Source: Internet
Author: User

Today, I saw an article in Lao Wang's blog titled "verifying the use of subqueries to improve MySQL paging efficiency ".Article, Very rewarding, summary and sharing ~

For MySQL tables with large data volumes, using limit paging has serious performance problems. For example, in a test conducted by Lao Wang, a table with nearly 10 million rows of records is used to query 30 records after 1,000,000th:

 

SQLCode1: Average Time: 6.6 seconds
Select * From 'cdb _ posts' order by PID limit 1000000, 30

SQL code 2: average 0.6 seconds
Select * From 'cdb _ posts' where pid> = (select PID from 'cdb _ posts' order by PID limit 1000000, 1) Limit 30

To retrieve the content of all fields, the first method must span a large number of data blocks and retrieve the content. The second method can retrieve the content directly based on the index field, which naturally improves the efficiency.

It can be seen that the offset of the limit statement will be larger and the speed gap between the two statements will become more obvious. In practical applications, you can use a method similar to the policy mode to process pages. For example, if the number of pages is less than one hundred, you can use the most basic paging method, with more than one hundred pages, the subquery page is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.