Code example of Oracle multi-Table query optimization

Source: Internet
Author: User

If you are a beginner in Oracle multi-Table query optimization and do not know much about the actual application of Oracle multi-Table query optimization, the following articles will provide you with more comprehensive knowledge. The following is a detailed description of the article.

Oracle uses the bottom-up sequence to parse the WHERE clause. According to this principle, the join between tables must be written before other WHERE conditions. The conditions that can filter out the maximum number of records must be written at the end of the WHERE clause.

For example:

(Inefficient, execution time: 156.3 seconds)

 
 
  1. SELECT …  
  2. FROM EMP E  
  3. WHERE SAL >; 50000  
  4. AND JOB = ‘MANAGER’  
  5. AND 25 < (SELECT COUNT(*) FROM EMP  
  6. WHERE MGR=E.EMPNO);  

(Efficient, execution time: 10.6 seconds)

 
 
  1. SELECT …  
  2. FROM EMP E  
  3. WHERE 25 < (SELECT COUNT(*) FROM EMP  
  4. WHERE MGR=E.EMPNO)  
  5. AND SAL >; 50000  
  6. AND JOB = ‘MANAGER’;  

Focus on 3: Avoid using '*' in the SELECT clause. Focus on

When you want to list all columns in the SELECT clause, using dynamic SQL COLUMN reference '*' is a convenient method. unfortunately, this is a very inefficient method. in fact, Oracle converts '*' into all column names in sequence during parsing. This task is completed by querying the data dictionary in Oracle Multiple tables, this means that it will take more time.

7. Reduce the number of database accesses

When each SQL statement is executed, Oracle performs a lot of internal work: parsing SQL statements, estimating index utilization, binding variables, and reading data blocks. it can be seen that reducing the number of visits to the database can actually reduce the workload of Oracle.

For example,

There are three ways to retrieve employees with employee numbers equal to 0342 or 0291.

Method 1 (most inefficient)

 
 
  1. SELECT EMP_NAME , SALARY , GRADE  
  2. FROM EMP  
  3. WHERE EMP_NO = 342;  
  4. SELECT EMP_NAME , SALARY , GRADE  
  5. FROM EMP  
  6. WHERE EMP_NO = 291;  

Method 2 (low efficiency)

 
 
  1. DECLARE  
  2. CURSOR C1 (E_NO NUMBER) IS  
  3. SELECT EMP_NAME,SALARY,GRADE  
  4. FROM EMP  
  5. WHERE EMP_NO = E_NO;  
  6. BEGIN  
  7. OPEN C1(342);  
  8. FETCH C1 INTO …,..,.. ;  
  9. OPEN C1(291);  
  10. FETCH C1 INTO …,..,.. ;  
  11. CLOSE C1;  
  12. END;  

The above content is an introduction to Oracle multi-Table query optimization. I hope you will find some gains.

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.