MySQL paging principle

The LIMIT clause can be used to force the SELECT statement to return the specified number of records. LIMIT accepts one or two numeric arguments. The argument must be an integer constant. If two parameters are given, the first parameter specifies the offset of the first returned record line, and the second parameter specifies the maximum number of returned record lines. The offset of the initial record line is 0 (instead of 1).

 

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

//To retrieve all rows from an offset to the end of the recordset, specify the second parameter as -1:
mysql> SELECT * FROM table LIMIT 95,-1; // Retrieve record row 96-last.

//If only one parameter is given, it means to return the maximum number of record rows:
mysql> SELECT * FROM table LIMIT 5; //Retrieve the first 5 record rows

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

As the amount of data increases, the number of pages will increase, that is, the further the paging is, the larger the offset of the LIMIT statement will be, and the speed will be significantly slower. At this point, we can improve the paging efficiency by sub-queries

 

SELECT * FROM articles WHERE  id >= (SELECT id FROM articles  WHERE
category_id = 123 ORDER BY id LIMIT 10000, 1) LIMIT 10  

Create a composite index on the category_id, id columns

 

http://www.jb51.net/article/46015.htm

http://qimo601.iteye.com/blog/1634748

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326531800&siteId=291194637