MYSQL, SQL Server, Oracle three kinds of database query paging method

Source: Internet
Author: User

SQL Server
There are many data about paging SQL, some use stored procedures, and some use cursors. I do not like the use of cursors, I think it cost, inefficient; Using stored procedures is a good choice because stored procedures are precompiled, efficient, and flexible. Let's look at the paged SQL for a single SQL statement.
Method 1:
Applies to SQL Server 2000/2005
Select Top Page Size * FROM table1 WHERE ID not in (SELECT top page Size * (page-1) ID from table1 ORDER by ID) Order by ID
Method 2:
Applies to SQL Server 2000/2005
Select Top Page Size * FROM table1 WHERE ID > (select ISNULL (MAX (ID), 0) from (select Top Page Size * (page 1) ID from table1 ORDER by ID) A) ORDER by ID
Method 3:
Applies to SQL Server 2005
Select TOP Page Size * FROM (select Row_number () over (ORDER by id) as rownumber,* from table1) A WHERE RowNumber > Page Size * (pages -1)
Description, Page size: Number of rows per page; pages: pages. When using, replace "page size" and "page size * (number of pages-1)" with digital.

MYSQL
SELECT * from TT LIMIT 1,20
SELECT * from TT LIMIT 21,30
/*
If you are thousands of tens of thousands of data, the direct use of the MySQL function limit of the normal usage of the OK, if it is more than 1 million of the data, it may be necessary to talk about the method, the following we do a millions data paged query statement.
Mysql> SELECT * FROM News where id>= (select ID from news limit 490000,1) limit 10; 0.18 sec//Obviously, this is the way to win.
Mysql> SELECT * FROM news limit 490000,10//0.22 sec;
*/
The following article mainly introduces the actual operation of MySQL paging, in fact, the simplest way to implement MySQL paging is to take advantage of the MySQL database limit function, limit [offset,] Rows can start retrieving n records from the MySQL database table with the first m record as:

SELECT * FROM table name LIMIT m,n
For example, from table sys_option (primary key to sys_id), 20 records are retrieved starting from the 10th record, with the following statement:
SELECT * FROM Sys_option limit 10,20
SELECT * FROM table [query condition] ORDER by ID limit?,?
Oracle
Oracle's paging query statements can basically follow this article, and the next article will look at examples. The following is a brief discussion of the multi-table syndication scenario. For the most visible equivalent table connection query, the CBO may generally use two connection modes nested loop and hash join (the MERGE join efficiency is less efficient than the hash join, and the general CBO will not consider it). Here, because paging is used, the maximum number of records to be returned is specified, and the NESTED loop can immediately contain and return the results to the center layer when the number of returned records crosses the maximum, and the hash join must process all the episodes (the MERGE join is also). In most cases, it is more efficient to select nested loop as the query connection method for paging queries (most of the time when paging queries are querying the first few pages of data, the smaller the number of pages accessed by the subsequent page).
Therefore, if you do not mind using hint in the system, you can rewrite the paging query statement to:
SELECT/*+ first_rows */* FROM
(
SELECT a.*, ROWNUM RN
From (SELECT * from table_name) A
WHERE ROWNUM <= 40
)
WHERE RN >= 21

MYSQL, SQL Server, Oracle three kinds of database query paging method

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.