Oracle Paging SQL statements

Source: Internet
Author: User

1. According to rowID to divide
SELECT * from T_xiaoxi where rowID in (the Select Rid from (select RowNum rn,rid from, select rowID rid,cid from

t_xiaoxi  ORDER BY cid Desc) where rownum<10000) where RN >9980) Order BY cid Desc;
execution time 0.03 seconds
2. Divide by analysis function
select * FROM (select T.*,row_number () over (order BY cid Desc) RK From T_xiaoxi T) where rk<10000 and rk>9980;
execution time 1.01 seconds
3. Press rownum to divide
select * FROM (select T.*,rownum rn from (SELECT * FROM T_xiaoxi order by cid Desc) t where rownum<10000) where

rn>9980; Execution Time 0.1 sec.
Where T_xiaoxi is the table name, CID is the key field of the table, the 第9981-9999条 record is sorted by CID descending, and the T_xiaoxi table has more than 70,000 records.
Personal feeling 1 efficiency best, 3 times, 2 worst

4.

Paged Query format:

SELECT *

From (SELECT a.*, ROWNUM RN

From (SELECT *

From table_name) a

WHERE ROWNUM <= 40)

WHERE RN >= 21

The most internal query, select * FROM table_name, represents the original query statement without paging. ROWNUM <= 40 and RN >= 21 control the range of pages per page for paged queries.

The paging query statement given above has high efficiency in most cases. The purpose of paging is to control the output result set size and return the results as soon as possible. In the above paged query statement, this consideration is mainly reflected in the where ROWNUM <= 40 sentence.

There are two ways to select the 21st to 40th record, one of which is shown in the above example in the second layer of the query through the rownum <= 40来 control The maximum value, at the outermost level of the query control the minimum value. The other way is to remove the ROWNUM <= 40 statement that queries the second layer, controlling the minimum and maximum paging values at the outermost level of the query. This is the query statement as follows:

SELECT *

From (SELECT a.*, ROWNUM RN

From (SELECT *

From table_name) a)

WHERE RN between and 40

In contrast to these two formulations, the first query is much more efficient than the second in most cases.

This is because in the CBO optimization mode, Oracle can push the outer query condition into the inner query to improve the execution efficiency of the inner query. For the first query statement, the second level of the query condition where ROWNUM <= 40 can be pushed into the inner query by Oracle, so that if the results of an Oracle query exceed the ROWNUM limit, the result is returned to the terminating query.

and the second query statement, because the query condition between and 40 is present in the third layer of the query, and Oracle cannot push the third layer of the query conditions to the most inner layer (even pushing to the inner layer is meaningless, because the most inner query does not know what RN represents). Therefore, for the second query statement, the Oracle's inner layer is returned to the middle tier as all data that satisfies the criteria, and the middle tier returns to the outermost of all the data. Data filtering is done at the outermost layer, and obviously this is much less efficient than the first query.

The query analyzed above is not just a simple query for a single table, but is as effective for the most inner query as a complex multi-table union query or a case in which the inner query contains a sort.

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.