Application of Oracle paging query statements

Source: Internet
Author: User

The actual use of the Oracle paging query statement can be applied according to the format given in the following article. The following describes the specific solution of the Oracle paging query statement. I hope it will be helpful in your future study.

Oracle paging Query format:

 
 
  1. SELECT * FROM  
  2. (  
  3. SELECT A.*, ROWNUM RN  
  4. FROM (SELECT * FROM TABLE_NAME) A  
  5. WHERE ROWNUM <= 40  
  6. )  
  7. WHERE RN >= 21 

SELECT * FROM TABLE_NAME indicates the original query statement that does not flip pages. ROWNUM <= 40 and RN> = 21 control the range of each page of the paging query.

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

There are two methods to select 21st to 40 records. One is that the second layer of the query in the example above uses ROWNUM <= 40 to control the maximum value, the minimum value is controlled at the outermost layer of the query. The other method is to remove the where rownum <= 40 Statement on the second layer of the query, and control the minimum and maximum paging values at the outermost layer of the query. Here is the query statement:

 
 
  1. SELECT * FROM  
  2. (  
  3. SELECT A.*, ROWNUM RN  
  4. FROM (SELECT * FROM TABLE_NAME) A  
  5. )  
  6. WHERE RN BETWEEN 21 AND 40 

In most cases, the efficiency of the first query is much higher than that of the second query.

This is because in the CBO optimization mode, Oracle can push the outer query conditions to the inner query to improve the execution efficiency of the inner query. For the first query statement, the SQL query condition WHERE ROWNUM <= 40 can be pushed to the inner query by Oracle. In this way, once the query result of Oracle exceeds the ROWNUM limit, the query is terminated and the result is returned.

The second query statement, because the query conditions BETWEEN 21 AND 40 exist on the third layer of the query, however, Oracle cannot push the layer-3 query conditions to the inner layer, which makes no sense even if it is pushed to the inner layer, because the inner layer query does not know what RN represents ).] Therefore, for the second query statement, the oldest layer of Oracle returns all the data that meets the conditions to the middle layer, and the data that the middle layer returns to the outermost layer is all the data. Data filtering is completed at the outermost layer. Obviously, this efficiency is much lower than the first query.

The query analyzed above is not only a simple query for a single table, but also effective for complex multi-table joint queries or sorting in the innermost query.

The query that contains sorting is not described here, and the next article will explain in detail through examples. Next we will briefly discuss the situation of Multi-table join. For the most common equi JOIN queries, CBO generally uses two JOIN Methods: nested loop and hash joinmerge join, which are less efficient than hash join. Generally, CBO does not consider this ).

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 stop and return the results to the intermediate layer, however, hash join must process all result set merge join ). In most cases, it is highly efficient to select nested loop as the query connection method for Oracle paging query. In most cases, the first few pages of data are queried, the lower the page number, the lower the chance of access ).

Therefore, if you don't mind using HINT in the system, you can rewrite the paging query statement:

 
 
  1. SELECT /*+ FIRST_ROWS */ * FROM  
  2. (  
  3. SELECT A.*, ROWNUM RN  
  4. FROM (SELECT * FROM TABLE_NAME) A  
  5. WHERE ROWNUM <= 40  
  6. )  
  7. WHERE RN >= 21  
  8. via  

The above content is the description of the relevant content applied to the Oracle paging query statement. I hope it will help you in this regard.

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.