The implementation of SQL paging sorting and the repeated paging data take Oraclerownum as an Example

Source: Internet
Author: User
For relational databases, It is very common to directly write SQL statements to pull data in the list. But this brings about a problem: when the data volume reaches a certain level, the system memory will sooner or later

For relational databases, It is very common to directly write SQL statements to pull data in the list. But this brings about a problem: when the data volume reaches a certain level, the system memory will sooner or later

For relational databases, It is very common to directly write SQL statements to pull data in the list. But this brings about a problem: when the data volume reaches a certain level, the system memory will consume sooner or later. In addition, network transmission is also a problem. If you have 10 million data records and want to see the last one, it will take an hour or two to transmit so much data over the network even if you have enough memory, i'm afraid few users have such patience. Therefore, pagination is required.

Currently, online forums, blogs, and so on basically all have paging functions, some of which are SQL paging, and some may be NOSQL paging using other methods, all of which are very mature. Based on my own experience, this article uses Oracle as an example to explain simple SQL paging and sorting problems. It will be helpful to those who are new to SQL preparation and paging, you don't have to read it.

Assume that the ORALCE database has a TAB001 table with the primary key ID, 10 million records, and indexes. We have a requirement to list records with specified conditions on the interface. The original SQL statement is as follows:

Select ID, NAME, ATYPE, CREATEDATE, CREATOR, ASTATUS from TAB001 where ATYPE = 'some _ type'

For example, to sort by CREATOR in reverse order, we will add the following statement after SQL: order by CREATEOR desc

Now, we find that there are 5 million records in this SQL statement. Obviously, the system will easily turn off without paging. So we prepare paging.

Before paging, we may need to display several buttons and statuses on the interface: previous Page, next page, first page, last page, X per page, total page M, current page N, jump to page N, etc. Obviously, the paging steps are as follows:

Calculates the total number of records;
Calculate the total number of pages based on the total number of records and the number of records per page;
Calculate the start and end records based on the current page number;
Generate a paging SQL statement. Execute the SQL statement and return the data on this page.
First, calculate the total number of records. This is simple. Just nest a select count:

Select count (*)
From (
Select ID, NAME, ATYPE, CREATEDATE, CREATOR, ASTATUS from TAB001 where ATYPE = 'some _ type'
) Xx

Then, the total number of pages = ceil (total number of records/number of records per page), less than one page is also processed as one page.

Next, assuming the N page is displayed, the start and end records on this page are:

Start Record Number = N * Number of records per page

End record number = min (N + 1) * Number of records per page-1, total number of records)

Finally, a paging SQL statement is generated. Because there must be a record number on the page, a subquery must be nested to generate ROWNUM:

Select rownum as recordno
From (
Select ID, NAME, ATYPE, CREATEDATE, CREATOR, ASTATUS from TAB001 where ATYPE = 'some _ type'
) Xx

In this way, we have a record number. We can filter the Record Number and select only the records after the Starting record number and before the ending record number on this page:

Select xxx .*
From (
Select rownum as recordno
From (
Select ID, NAME, ATYPE, CREATEDATE, CREATOR, ASTATUS from TAB001 where ATYPE = 'some _ type'
) Xx
) Xxx
Where recordno> =: Start Record Number
And recordno <=: End Record Number

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.