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.