MySQL implement paging - based limit mode

A, limit the basic implementation

In general, the client by passing pageNo is not (page), the pageSize (per number of) two paging parameters to query data in the database using MySQL comes in a smaller amount of data (tuple hundred / one thousand) of  limit to solve this problem:

A client that receives pageNo is not {: 1 , pageSize: 10 } 
 2  SELECT * from Table limit (pageNo- 1 ) * the pageSize, the pageSize;
 . 3  
. 4 receives client pageNo is not {: . 5 , the pageSize: 30 } 
 . 5  SELECT * from Table limit (pageNo- . 1 ) * the pageSize, the pageSize;

 

Second, the establishment of a primary key or unique index

In the small amount of data when using a simple  limit data paging performance of the above will not significantly slow, but the amount of data reaching 10 000 to one million performance sql statement will affect the data returned. In this case need to use a unique primary key or index data page;

1  assume that the primary key or a unique index good_id 
 2 receives the client pageNo is not {: . 5 :, pageSize 10 } 
 . 3  SELECT * from Table WHERE good_id> (pageNo- 1 ) * ; the pageSize limit the pageSize 
 . 4 between 40 to return good_id 50-- The data

 

Third, based on the data reordering

When the information required for the return or descending order, based on the above statement data reordering. order by ASC / DESC order or descending order by default

SELECT * from Table WHERE good_id> (pageNo- . 1 ) * the pageSize Order by good_id limit the pageSize; 
- returns data good_id between 40 to 50, according to the data arrangement order good_id

 

Fourth, the paging best

1  10 per:
 2 Current 1 18  120 , 125 
. 3  
. 4  reverse:
 5              size
 . 6              980     970   . 7  . 6   . 6  5   54 is   43 is   32 
. 7  
. 8  21 is  . 19  98      
. 9  Next:
 10  
. 11      SELECT  
12 is          * 
 13 is      from  
14          TB1 
 15      WHERE  
16          NID <( SELECT NID from ( SELECT NID fromTB1 WHERE NID <minimum value of this page order by nid desc limit per data * p [- this page]) by A.nid ASC Order A limit . 1 )  
 . 17      Order by 
 18 is          NID desc 
 . 19      limit 10 ;
 20 is  
21 is  
22 is  
23 is      SELECT  
24          * 
 25      from  
26 is          TB1 
 27      WHERE  
28          NID <( SELECT NID from ( SELECT NID from TB1 WHERE NID < 970   Order by NID desc limit 40) A limit Order by A.nid ASC . 1 )  
 29      Order by 
 30          NID desc 
 31 is      limit 10 ;
 32  
33 is  
34 is  Previous:
 35  
36      SELECT  
37 [          * 
 38 is      from  
39          TB1 
 40      WHERE  
41 is          NID <( SELECT NID from ( SELECT NID from TB1 WHERE NID> this page order by nid asc limit the maximum data page this page [* - p]) by A.nid ASC Order A limit . 1 )  
 42 is      Order by 
43         nid desc 
44     limit 10;
45 
46 
47     select 
48         * 
49     from 
50         tb1 
51     where 
52         nid < (select nid from (select nid from tb1 where nid > 980 order by nid asc limit 20) A order by A.nid desc limit 1)  
53     order by 
54         nid desc 
55     limit 10;

Guess you like

Origin www.cnblogs.com/june-L/p/12150508.html