Quickly Master the "MySQL limit" Operation Process

Source: Internet
Author: User

MySQL limit operation:

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, you can 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?

If this is the case, the error is returned. The result is: 1 row in SET (8.88)
SEC, how is it? It's scary. It reminds me of the "high score" we had in 4.1 yesterday ". select * it is best not to use it casually. It should be based on the principle of what to use and what to choose,
The more fields you select, the more data the field has, 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 classified as follows:

90000 rows in SET (0.36) sec

1 row in SET (0.06) sec

Note: SQL server can only use select top 90000 ID from Cyclopedia for 390 ms, and the execution time is inferior to that of MySQL for 360 ms.

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.