Oracle
The paging query statement of Oracle can basically follow this article. The next article will explain it through examples. Next we will briefly discuss the situation of Multi-table join. For join queries of the most common equivalent tables, CBO may use two join Methods: nested loop and hash join (merge join is less efficient than hash join, which is not considered by CBO in general ). Because paging is used, the maximum number of records returned is specified. When the number of returned records exceeds the maximum value, the nested loop can immediately curb and return the results to the central layer, however, hash join must process all the replica sets (so does merge join ). In most cases, select nested for the paging query.
Loop as the connection method for query is highly efficient (when querying by page, the vast majority of cases are the data of the first few pages, and the smaller the page access probability is ).
Therefore, if you don't mind using hint in the system, you can rewrite the paging query statement:
Select/* + first_rows */* from
(
Select a. *, rownum Rn
From (select * From table_name)
Where rownum <= 40
)
Where rn> = 21
SQL Server
There are a lot of information about paging SQL, some use stored procedures, and some use cursors. I don't like using cursors. I think they are cost-effective and inefficient. Using Stored procedures is a good option, because stored procedures are pre-compiled at the end of the process, and execution efficiency is high, it is also more flexible. Let's take a look at the paging SQL of a single SQL statement.
Method 1:
Applicable 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:
Applicable 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:
Applicable 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 * (page number-1)
Note: page size: the number of lines per page; page number: page number. During use, replace "page size" and "page size * (page size-1)" with a digital number.
MySQL
Select * from TT limit 1, 20
Select * from TT limit 21,30
/*
If you have tens of thousands of data, you can directly use the Limit Function of MySQL. If it is more than 1 million of data, you may have to explain the method, next we will create a paging query statement for millions of data records.
Mysql> select * from news where ID> = (select ID from news limit 490000, 1) limit 10; // 0.18 sec // obviously, this method wins.
Mysql> select * from news limit 490000,10 // 0.22 sec;
*/
The following article mainly introduces the actual MySQL paging operation solution. In fact, the simplest way to implement MySQL paging is to use the MySQL database limit function, limit [offset,] rows can search for N records starting from the M records in the MySQL database table:
- Select * from table name limit m, n
For example, to retrieve 20 records from the table sys_option (primary key: sys_id) starting from the first 10th records, the statement is as follows:
- Select * From sys_option limit 10, 20
- Select * from table [query condition] Order by ID limit ?,?
Oracle
The paging query statement of Oracle can basically follow this article. The next article will explain it through examples. Next we will briefly discuss the situation of Multi-table join. For join queries of the most common equivalent tables, CBO may use two join Methods: nested loop and hash join (merge join is less efficient than hash join, which is not considered by CBO in general ). Because paging is used, the maximum number of records returned is specified. When the number of returned records exceeds the maximum value, the nested loop can immediately curb and return the results to the central layer, however, hash join must process all the replica sets (so does merge join ). In most cases, select nested for the paging query.
Loop as the connection method for query is highly efficient (when querying by page, the vast majority of cases are the data of the first few pages, and the smaller the page access probability is ).
Therefore, if you don't mind using hint in the system, you can rewrite the paging query statement:
Select/* + first_rows */* from
(
Select a. *, rownum Rn
From (select * From table_name)
Where rownum <= 40
)
Where rn> = 21
SQL Server
There are a lot of information about paging SQL, some use stored procedures, and some use cursors. I don't like using cursors. I think they are cost-effective and inefficient. Using Stored procedures is a good option, because stored procedures are pre-compiled at the end of the process, and execution efficiency is high, it is also more flexible. Let's take a look at the paging SQL of a single SQL statement.
Method 1:
Applicable 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:
Applicable 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:
Applicable 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 * (page number-1)
Note: page size: the number of lines per page; page number: page number. During use, replace "page size" and "page size * (page size-1)" with a digital number.
MySQL
Select * from TT limit 1, 20
Select * from TT limit 21,30
/*
If you have tens of thousands of data, you can directly use the Limit Function of MySQL. If it is more than 1 million of data, you may have to explain the method, next we will create a paging query statement for millions of data records.
Mysql> select * from news where ID> = (select ID from news limit 490000, 1) limit 10; // 0.18 sec // obviously, this method wins.
Mysql> select * from news limit 490000,10 // 0.22 sec;
*/
The following article mainly introduces the actual MySQL paging operation solution. In fact, the simplest way to implement MySQL paging is to use the MySQL database limit function, limit [offset,] rows can search for N records starting from the M records in the MySQL database table:
- Select * from table name limit m, n
For example, to retrieve 20 records from the table sys_option (primary key: sys_id) starting from the first 10th records, the statement is as follows:
- Select * From sys_option limit 10, 20
- Select * from table [query condition] Order by ID limit ?,?