MySQL limit operation

Source: Internet
Author: User
Select * from Table limit 5, 10; # return data in rows 6-15
Select * from Table limit 5; # Return the first five rows
Select * from Table limit; # Return the first five rows

Performance Optimization:

Based on the high performance of limit in mysql5.0, I have a new understanding of data paging.

1.
Select * From Cyclopedia where ID> = (
Select max (ID) from (
Select ID from Cyclopedia order by ID limit 90001
) As TMP
) Limit 100;

2.
Select * From Cyclopedia where ID> = (
Select max (ID) from (
Select ID from Cyclopedia order by ID limit 90000,1
) As TMP
) Limit 100;

Is it faster to get 90000 records after 100, 1st sentences or 2nd sentences?
The first 1st records are obtained first, and the largest ID value is used as the start ID. Then, the first 90001 records can be quickly located.
The 2nd clause selects only the first record after the first record, and then takes the id value as the starting marker to locate the next 90000 records.
1st statement execution result. 100 rows in SET (0.23) sec
2nd statement execution result. 100 rows in SET (0.19) sec

Obviously, 2nd sentences won. it seems that limit does not seem as much as I previously imagined to do a full table scan and return limit Offset + length records, so it seems that limit is much better than the top performance of the MS-SQL.

In fact, 2nd sentences can be simplified

Select * From Cyclopedia where ID> = (
Select ID from Cyclopedia limit 90000,1
) Limit 100;

Using the IDs of 90,000th records directly does not require the max operation. In this way, the theoretical efficiency is higher, but the results are almost invisible in actual use, because the ID returned by the positioning itself is a record, Max can get the result almost without operation, but this write is clearer, saving the time to draw a snake.

However, since MySQL has a limit that can directly control the location where records are retrieved, why not simply use select * From Cyclopedia limit, 1? Isn't it more concise?
I thought it would be wrong. After I tried it, I knew that the result was: 1 row in SET (8.88) Sec. How is it so scary, it reminds me of the "high score" I had in 4.1 yesterday ". select * it is best not to use it casually. Based on the principle of "what to use" and "what to choose", the more select fields, the larger the field data volume, the slower the speed. which of the above two paging methods is much better than the single-write method? Although it seems that the number of queries is more, it is actually at a lower cost in exchange for efficient performance, it is very worthwhile.

The 1st schemes can also be used for MS-SQL, and may be the best, because it is always the fastest to locate the start segment by the primary key ID.

Select Top 100 * From Cyclopedia where ID> = (
Select top 90001 max (ID) from (
Select ID from Cyclopedia order by ID
) As TMP
)

But whether the implementation method is stored in the process or the direct code, the bottleneck is always that the top of the MS-SQL is always to return the first N records, this situation in the amount of data is not deep, but if hundreds of thousands, efficiency will definitely be low. in contrast, MySQL limit has many advantages. Execute:
Select ID from Cyclopedia limit 90000
Select ID from Cyclopedia limit 90000,1
The results are as follows:
90000 rows in SET (0.36) sec
1 row in SET (0.06) sec
The MS-SQL can only use select top 90000 ID from Cyclopedia for execution time is 390 ms, the same operation time is less than MySQL 360 ms.

Related Article

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.