Limit Usage in MYSQL _ MySQL

Source: Internet
Author: User
MYSQL limit usage
Mysql limit usage: when we use a query statement, we often need to return the first few or a few rows of data. what should we do at this time? Don't worry, mysql already provides us with such a function. SELECT * FROM table LIMIT [offset,] rows | the rows OFFSET offsetLIMIT clause can be used to force the SELECT statement to return the specified number of records. LIMIT accepts one or two numeric parameters. The parameter must be an integer constant. If two parameters are specified, the first parameter specifies the offset of the first returned Record Row, and the second parameter specifies the maximum number of returned record rows. The OFFSET of the initial Record Row is 0 rather than 1. to be compatible with PostgreSQL, MySQL also supports syntax: LIMIT # OFFSET #. Mysql> SELECT * FROM table LIMIT 5, 10; // retrieve records row 6-15 // to retrieve all record rows FROM an offset to the end of the record set, you can specify the second parameter-1:
Mysql> SELECT * FROM table LIMIT 95,-1; // Retrieve The Record Row 96-last. // if only one parameter is specified, it indicates that the maximum number of record rows is returned:
Mysql> SELECT * FROM table LIMIT 5; // retrieve the first five record rows // In other words, LIMIT n is equivalent to LIMIT 0, n. Note the differences between limit 10 and limit 9 and 1: for example, 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 the same as taking the first 90000 records after the first 100 records, the first sentence is faster or the second sentence is faster?
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 actually 2nd sentence can be simplified to: Select * From cyclopedia Where ID> = (
Select ID From cyclopedia limit 90000,1
) Limit 100; the ID of the first 90,000th records is used directly without the Max operation. this is a theoretical efficiency because it is higher, but it is hardly effective 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. select Top 100 * From cyclopedia Where ID> = (
Select Top 90001 Max (ID) From (
Select ID From cyclopedia Order By ID
) As tmp
) But whether it is the implementation of the stored process or 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 big, however, if there are hundreds of thousands, the 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 execution time is 390 ms, the execution of the same operation time is less than MySQL 360ms. the offset of limit is used when there are too many records, and the offset is small. it is better to use limit directly. The greater the offset, the better the latter. //////////////////////////////////////// ////////////////////////////////1. when the offset value is small.

Select * from yanxue8_visit limit 10, 10
Run multiple times and keep the time between 0.0004 and 0.0005
Select * From yanxue8_visit Where vid> = (
Select vid From yanxue8_visit Order By vid limit 10, 1
) Limit 10
Run multiple times, and the time is kept between 0.0005-0.0006, mainly 0.0006
Conclusion: When the offset is small, it is better to use limit directly. This display is the cause of the subquery.


2. when the offset value is large.
Select * from yanxue8_visit limit, 10
Run multiple times and keep the time at around 0.0187
Select * From yanxue8_visit Where vid> = (
Select vid From yanxue8_visit Order By vid limit, 1
) Limit 10
Run multiple times, with a time of around 0.0061, only 1/3 of the former. The larger the offset value, the better the latter. //////////////////////////////////////// //////////////////////////////////////// /// // Mysql> SELECT * FROM table LIMIT 95, -1; // Retrieve The Record Row 96-last.
// If only one parameter is specified, it indicates the maximum number of record rows returned.


Reprinted from: http://blog.csdn.net/zhqingyun163/article/details/5053579

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.