Mysql paging query statement Database Query

Source: Internet
Author: User

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, it is highly efficient to select nested loop as the query connection method for paging queries (most of the time when querying by page is the data of the first few pages, the lower the page number, the lower the access probability ).
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

Author: ERDP Technical Architecture"

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.