) SQL limit

Source: Internet
Author: User

Select * from Table limit [offset,] rows | rows offset

Mysql> select * from Table limit 5, 10; // retrieves records from 6 to 15 rows.

// 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; // retrieves 96-last records.

// If only one parameter is specified, it indicates the maximum number of record rows returned:
Mysql> select * from Table limit 5; // retrieve the first five record rows

// In other words, limit N is equivalent to limit 0, N.

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

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 is obviously the cause of 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. It can be predicted that the larger the offset, the higher the latter.

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?
This is wrong. If you try it, you will know that the result is: 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 it is the storage process or direct Code In, 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 feeling deep, but if hundreds of thousands, efficiency will certainly 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.

----------------------------------------------------------

Limit thinking

On percona performance Conference 2009, several Yahoo engineers brought a "efficient pagination using MySQL" report, which has many highlights. This article is a further extension based on the original article.

First, let's take a look at the basic principles of paging:

Mysql> explain select * From message order by id desc limit 10000, 20g
* *************** 1. Row **************
ID: 1
Select_type: simple
Table: Message
Type: Index
Possible_keys: NULL
Key: Primary
Key_len: 4
Ref: NULL
Rows: 10020
Extra:
1 row in SET (0.00 Sec)

Limit limit, 20 means to scan 10020 rows that meet the condition, discard the first 10000 rows, and return the last 20 rows. The problem is here. If it is limit 100000,100, You need to scan 100100 rows, in a highly concurrent application, more than rows need to be scanned for each query, and the performance is definitely compromised. The performance of limit N is okay because only N rows are scanned.

This article introduces a "clue" approach to provide some "clues" for turning pages, such as select * From message order by id desc, paging by ID in descending order, 20 entries per page, currently, there are 10th pages. The maximum ID of the current page is 9527, and the minimum is 9500, if we only provide jumps such as "Previous Page" and "next page" (do not provide jumps to page N), the SQL statement can be:

Select * From message where ID> 9527 order by id asc Limit 20;

When processing the next page, the SQL statement can be:

Select * From message where ID <9500 order by id desc Limit 20;

No matter how many pages are displayed, only 20 rows are scanned for each query.

The disadvantage is that only "previous" and "Next" links can be provided, however, our product manager is very fond of "<Previous Page 1 2 3 4 5 6 7 8 9 next page>". What should I do?

If limit m and n are unavoidable, We need to optimize efficiency by minimizing M as much as possible. We need to extend the previous "clue" approach, or select * From message order by id desc, page by ID in descending order. There are 20 entries per page. Currently, there are 10th pages. The maximum ID of the current page is 9527, and the minimum is 9500. For example, to jump to 8th pages, I can write the SQL statement as follows:

Select * From message where ID> 9527 order by id asc Limit 20, 20;

Jump to page 13th:

Select * From message where ID <9500 order by id desc limit;

The principle is the same. It records the maximum and minimum values of the current page ID, and calculates the relative offset between the jump page and the current page. Because the page is similar, this offset is not very large, in this case, the m value is relatively small, greatly reducing the number of scanned rows. In fact, the relative offset of the traditional limit M, N has always been the first page. The more we move to the next page, the worse the efficiency, and the above method does not have such a problem.

Note the ASC and desc in the SQL statement. If the result is obtained by ASC, remember to put it upside down when it is displayed.

It has been tested in a table with a total data volume of 60 million, and the effect is very obvious.

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.