In mysql, limit is used to calculate the offset records from X to Y. For example, there are 100 records in our database, if I want to get the first 20 results, I can directly limit to get the results we want. Next I will introduce the usage of linut in detail.
Mysql limit syntax
The Code is as follows: |
Copy code |
SELECT * FROM table LIMIT [offset,] rows | rows 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. 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 #.
The Code is as follows: |
Copy code |
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. |
The above practice is still very fast in a small amount of data, but the tens of thousands of data sheets will not work. We can optimize limit as follows:
The results of my current million data tests are as follows:
The Code is as follows: |
Copy code |
Select * From yanxue8_visit Where vid> = (Select vid From yanxue8_visit Order By vid limit 10, 1) limit 10 Run, the time is kept between 0.0005-0.0006, mainly 0.0006 Select * from yanxue8_visit limit, run for more than 10 times, and keep the time 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.
Combined with php paging instances
The Code is as follows: |
Copy code |
<Html> <Head> <Title> ShowData </Title> </Head> <Body> <H2> ShowData <? Php // Connect to the database $ Page = 1; $ Db = mysql_connect ('2017. 0.0.1 ', 'root', 'toor '); Mysql_select_db ('test', $ db );
$ Pagesize = 3; // The number displayed on each page
// Calculate the total number of records used to calculate the number of pages $ Rs = mysql_query ("select count (*) from info", $ db ); $ Row = @ mysql_fetch_array ($ rs ); $ Numrows = $ row [0];
// Calculate the page number $ Pages = intval ($ numrows/$ pagesize ); If ($ numrows % $ pagesize) { $ Pages ++; } // Set the page number If (isset ($ _ GET ['page']) { $ Page = intval ($ _ GET ['page']); } Else { $ Page = 1; // in other cases, all point to the first page }
// Calculate the offset of the record $ Offset = $ pagesize * ($ page-1 ); // Read the specified record $ Rs = mysql_query ("select * from info order by id limit $ offset, $ pagesize", $ db ); // Display the data in a table If ($ row = @ mysql_fetch_array ($ rs )) { $ I = 0; ?> <Table border = '0' width = '000000'> <Tr> <Td width = '000000'> <P align = 'center'> ID </td> <Td width = '000000'> <P align = 'center'> DOC </td> </Tr> <? Php Do { $ I ++; ?> <Tr align = 'center'> <Td width = '000000'> <? = $ Row ['id']?> </Td> <Td width = '000000'> <? = $ Row ['Doc']?> </Td> </Tr> <? Php } // Display data cyclically While ($ row = mysql_fetch_array ($ rs )); Echo "</table> "; } Echo "<div align = 'center'> total". $ pages. "page (". $ page. "/". $ pages .")"; For ($ I = 1; $ I <$ page; $ I ++) { Echo "<a href = 'showdata. php? Page = ". $ I." '> [". $ I."] </a> "; } Echo "[". $ page. "]"; For ($ I = $ page + 1; $ I <= $ pages; $ I ++) { Echo "<a href = 'showdata. php? Page = ". $ I." '> [". $ I."] </a> "; } Echo "</div> "; ?> </Body> </Html> |