Thoughts on implementing the paging function using the inverted top paging algorithm (implemented by Java code)

Source: Internet
Author: User

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

  1. Package com. Test. util
  2. Public class page (){
  3. Private int pagesize; // number of records on one page
  4. Private int currentpage; // current page
  5. Private int rowcount; // The total number of records.
  6. Private int pagecount; // the total number of pages calculated
  7. // Variable Initialization
  8. Public page (INT currentpage, int rowcount ){
  9. Pagesize = 10;
  10. Pagecount = rowcount/pagesize + 1; // calculate the total number of pages
  11. This. currentpage = currentpage;
  12. This. rowcount = rowcount;
  13. }
  14. // Returns the total number of records.
  15. Public int getpagecount (){
  16. Return pagecount;
  17. }
  18. // Return the number of records per page
  19. Public int getpagesize (){
  20. Return pagesize;
  21. }
  22. // Return the judgment status. If the first page returns 0, the last page returns 1, and if the normal page returns 2
  23. Public int checkcurrent (){
  24. If (current = 1 ){
  25. Return 0;
  26. }
  27. If (current = pagecount ){
  28. Return 1;
  29. }
  30. Else {
  31. Return 2;
  32. }
  33. }
  34. }

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.