1. Select a paging algorithm
The following is an inverted top paging algorithm.
Pagesize: number of records displayed per page
Cureentpage: Current page number
Select * from (select top pagesize * cureentpage * from news order by addtime DESC) as a order by addtime ASC) as B order by addtime DESC
In this way, the data on each page can be returned, and the index can improve the query efficiency.
Select count (*) from news
Total number of returned records
Ii. Program Design
Page. Java
- Package com. Test. util
- Public class page (){
- Private int pagesize; // number of records on one page
- Private int currentpage; // current page
- Private int rowcount; // The total number of records.
- Private int pagecount; // the total number of pages calculated
- // Variable Initialization
- Public page (INT currentpage, int rowcount ){
- Pagesize = 10;
- Pagecount = rowcount/pagesize + 1; // calculate the total number of pages
- This. currentpage = currentpage;
- This. rowcount = rowcount;
- }
- // Returns the total number of records.
- Public int getpagecount (){
- Return pagecount;
- }
- // Return the number of records per page
- Public int getpagesize (){
- Return pagesize;
- }
- // Return the judgment status. If the first page returns 0, the last page returns 1, and if the normal page returns 2
- Public int checkcurrent (){
- If (current = 1 ){
- Return 0;
- }
- If (current = pagecount ){
- Return 1;
- }
- Else {
- Return 2;
- }
- }
- }
Call in action
Int currentpage = integer. parseint (request. getparameter ("page "));
String SQL = "select count (*) from News ";
Resultset rs = stms.exe cutequery (SQL );
Rs. Next ();
Int rowcount = Rs. getint (1 );
Rs. Close ();
Page P = new page (currentpage, rowcount );
Int pagecount = P. getpagecount ();
Int flag = P. checkcurrent ();
Int pagesize = P. getpagesize ();
Resultset RS1 = NULL;
If (flag = 0 ){
SQL = "select top" + pagesize + "* from news order by addtime DESC ";
RS1 = stms.exe cutequery (SQL );
....................
}
If (flag = 1 ){
Int pagelast = rowcount-(pagecount-1) * pagesize );
SQL = "select * from (select top" + pagelast + "* from news order by addtime ASC) as a order by addtime DESC ";
RS1 = stms.exe cutequery (SQL );
....................
}
Else {
Int pagelast = rowcount-(pagecount-1) * pagesize );
SQL = "select * from (select top pagesize * cureentpage * from news order by addtime DESC) as a order by addtime ASC) as B order by addtime DESC ";
RS1 = stms.exe cutequery (SQL );
....................
}
.....................
The above is my paging function implemented by using the inverted top method, which is very simple.
In terms of efficiency, it should be no problem to Support 2 million pieces of data.
PS: will it be more efficient to apply SQL queries to stored procedures?