Common Database Paging

Source: Internet
Author: User

1. Oracle Paging

The first: Using the Analytic function Row_number () method

SELECT * FROM (
Select T.*,row_number () over (order by t1.id) Rowno from TABLE1
)
where rowno between and 40;

Second: Direct use of rownum virtual column ( recommended )

SELECT * FROM
(select T.*,rownum as Rowno from TABLE1)
where Rowno between and 20

Third: Do not use between and for paging

SELECT * FROM

(SELECT a.*, RowNum R

From

(SELECT * from Msr_topic

Where name like%hr%

) A

WHERE rownum <= Indexend

) B

WHERE r > indexstart;

Summary: Obviously the second method is better, the third method is the worst! Because the order BY statement is not used, it can improve the speed of retrieving data, especially when the data volume is larger, the second method is more obvious to retrieve data quickly. the paging with order by is used with caution in Oracle. Especially in oracle10g, there will be confusion, that is, the same record will appear on different pages.


2. SQL Server Paging

First method: implemented by the top function ( recommended )

SELECT TOP Ten * from TestTable
WHERE (
ID not in (SELECT TOP $ * from testtable ORDER by ID)
)
ORDER by ID

The second approach: using the Max function to implement

Select Top Limit *
From table1
where id>
(select Max (ID) from
(select Top ((page-1) *limit) ID from the Table1 ORDER by ID) as T
)
ORDER BY ID

The third method: through the Row_number function +top to achieve


From
(
SELECT Top Limit Row_number () over (ORDER by ReportID) as RowNo
From TABLE
) as A
WHERE RowNo > page*10

Fourth method: by Row_number function +between and to implement

SELECT * FROM (SELECT * Row_number () + (order by u_id) as row_id from the users where u_name like%hr%) As Temp_userswhere row_id between ${start} and ${end}


Summarize:

1) The first method has a fatal disadvantage, that is, it contains not the word, to replace with not exists instead of in;

2) The second method note that the ID must be unique, cannot use a composite primary key, and the primary key must also be a regular number, otherwise it cannot be compared

3) The third method, Row_number (), is to generate a sequential line number, and the standard for his generation order is the next-to-last-order by ReportID, where ReportID can be a federated primary key. Needless to say, the worst way to bring your own function is not to use it.

4) as in the third way, inefficient

3. mysql Paging


SELECT * FROM table limit start, size


4. PostgreSQL Paging


SELECT * FROM table limit size offset start


Reference:

Many blogs ...


Common Database Paging

Related Article

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.