Tips for optimizing mysqllimit query statements

Source: Internet
Author: User
In mysql, limit is used for paging query. This article introduces the optimization techniques of mysqllimit query statements to coders. For more information about coders, see. When our website reaches a certain scale, various website optimizations are required. In the optimization of websites, database optimization is the most important. The author will share with you the optimization of limit statements in MySQL database query statements.

As we all know, limit is generally used on the pages of paging programs. when your application data volume is small enough, you may not feel any problems with the limit statement, however, when the query data volume reaches a certain level, the limit performance will drop sharply. This is the conclusion of a large number of instances.

The following example shows that 10 data entries are obtained for the same table in different places:

1) When offset is relatively small

select * from user limit 10,10;

This SQL statement is run multiple times, and the time is between 0.0004 and 0.0005.

Select * From user Where uid >=( Select uid From user Order By uid limit 10,1 ) limit 10;

This SQL statement is run multiple times, and the time is between 0.0005 and 0.0006, mainly 0.0006.

Conclusion: When the offset is small, it is better to use limit directly. This is obviously the cause of subquery.

(2) when the offset is large

select * from user limit 10000,10;

This SQL statement is run multiple times, with a time of around 0.0187

Select * From user Where uid >=( Select uid From user Order By uid limit 10000,1 ) limit 10;

This SQL statement is run multiple times, with a time of around 0.0061, only 1/3 of the former. It can be predicted that the larger the offset, the higher the latter.

After comparison, we come to the conclusion that when using the limit statement, you can directly use limit when the data volume offset is small. when the data volume offset is large, we can use subqueries to optimize the performance.

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.