Mysql limit the use of detailed explanation

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags data direct function how to how to do multiple multiple times mysql

Mysql limit usage: When we use the query, often to return to the first few or a few lines in the middle of the data, how to do this time? Do not worry, mysql has provided us with such a function.


SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset

The LIMIT 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. Given two parameters, the first parameter specifies the offset of the first record row, and the second parameter specifies the maximum number of record rows to return. The offset of the initial record line is 0 (not 1): MySQL also supports the syntax LIMIT # OFFSET # for compatibility with PostgreSQL.

mysql> SELECT * FROM table LIMIT 5,10; / / retrieve records 6-15

// To retrieve all rows from one offset to the end of the recordset, specify the second argument to -1:

mysql> SELECT * FROM table LIMIT 95, -1; // retrieve record line 96-last.

// If only one parameter is given, it means that the maximum number of record rows is returned:

mysql> SELECT * FROM table LIMIT 5; / / Retrieve the first five records

// In other words, LIMIT n is equivalent to LIMIT 0, n.

Note the difference between limit 10 and limit 9,1:

E.g:

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;

The same is to take 90000 after 100 records, the first sentence fast or the second sentence fast?

The first sentence is to take the first 90001 records, whichever is the largest of the ID value as a starting identification, and then use it to quickly locate the next 100 records

The second sentence is to choose only take 90000 records after a 1, and then take the ID value for the initial identification under the positioning of 100 records

The first sentence execution result .100 rows in set (0.23) sec

The second sentence execution result .100 rows in set (0.19) sec

In fact, the second sentence can be simplified as:


Select * From cyclopedia Where ID> = (
Select ID From cyclopedia limit 90000,1
) limit 100;

The direct use of the first record of the ID 90000, without Max computing, in theory, because of the higher efficiency, but in actual use almost can not see the effect, because the return ID itself is a record ID, Max almost no operation Will be able to get the result, but so clear and clearer to write, eliminating the need to draw a snake that foot.


Select Top 100 * From cyclopedia Where ID> = (
Select Top 90001 Max (ID) From (
Select ID From cyclopedia Order By ID
) As tmp
)

However, whether the implementation is stored procedure or direct code, the bottleneck is always MS-SQL TOP always returns the first N records, this situation in the small amount of data not feel deep, but if hundreds Million, the efficiency will certainly be low. In contrast, the limit of MySQL there are more advantages, the implementation of:


Select ID From cyclopedia limit 90000
Select ID From cyclopedia limit 90000,1

The results are:


90000 rows in set (0.36) sec
1 row in set (0.06) sec

The MS-SQL only Select Top 90000 ID From cyclopedia execution time is 390ms, perform the same operation time is less than MySQL 360ms.

Limit offset (offset) for more records, less records, the offset offset is smaller, the direct use of limit better. The larger the offset, the better the latter.

1, offset relatively small time.

select * from yanxue8_visit limit 10,10

Running multiple times, the time remained between 0.0004-0.0005


Select * From yanxue8_visit Where vid> = (
Select vid From yanxue8_visit Order By vid limit 10,1
limit 10

Running multiple times, the time remained between 0.0005-0.0006, mainly 0.0006

Conclusion: Offset offset smaller, the direct use of limit better. This display is the reason for the subquery.

2, offset big time.

select * from yanxue8_visit limit 10000,10

Running multiple times, the time remained at 0.0187 or so

Select * From yanxue8_visit Where vid> = (

Select vid From yanxue8_visit Order By vid limit 10000,1

limit 10

Running multiple times, the time remained at around 0.0061, only the former one-third. The larger the offset, the better the latter.

mysql> SELECT * FROM table LIMIT 95, -1; // retrieve record line 96-last.

// If only one parameter is given, it means that the maximum number of records is returned

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.