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 multiple run times, the time is kept between 0.0004-0.0005 select * From yanxue8_visit where vid> = ( Select vid from yanxue8_visit order by VID limit 10, 1 ) Limit is run for more than 10 times, with the time between 0.0005 and 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, run for more than 10 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 is run more than 10 times, and the time is kept at 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. |