Paging is often used when displaying record entries, and a common approach is to overwrite the original query statements with the various database-based positioning interfaces, so that only certain records of a particular range are taken out. Different databases, the query location interface is not the same, the following do a summary:
Database |
Paged Query Statements |
Description |
Mysql |
"query_sql limit?,?" |
Use the Limit keyword, the first "?" is the starting line number, A second "?" Is the number of returned entries |
Oracle |
SELECT * FROM (SELECT a.*, ROWNUM RN from (query_sql ) A WHERE ROWNUM <=? ) WHERE RN >=? |
Combined with rownum keyword, using nested three-layer Select Statement implementation. The first one "?" Indicates the terminating line number, A second "?" Indicate its implementation number |
SQL Server |
No general statements yet |
You can use top N to return to the top N records or use stored procedures |
DB2 |
Hypothetical query statement: Select t1.* from T1 order by T1.id; The paging statement can be: "SELECT * FROM (select RowNumber () (Order by T1.id) as Row_, t1.* from T1 Order by T1.id) as Temp_ where Row_ Between? +1 and? " |
Returns two "?" The record between |
InterBase |
"query_sql row?" To? " |
Returns two "?" The record between |
PostgreSQL |
"query_sql limit?" Offset? " |
The first one "?" is the starting line number, and the second "?" Represent Returns the number of records
|
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
Various database paging query SQL statement Daquan