Oracle SQL Performance Optimization series (10)

Source: Internet
Author: User

31. Forced index failure

 

If two or more indexes have the same level, you can force the ORACLE optimizer to use one of them (using it, the number of retrieved records is small ).

 

Example:

SELECT ENAME

FROM EMP

Where empno = 7935

And deptno + 0 = 10/* DEPTNOThe index on will be invalid */

AND EMP_TYPE | ''= 'A'/* EMP_TYPEThe index on will be invalid */

 

This is a very direct way to improve query efficiency, but you must consider this strategy with caution. Generally, you can use it only when you want to optimize several SQL statements separately.

 

Here is an example about when to adopt this policy,

 

Assume that there is a non-unique index on the EMP_TYPE column of the EMP table, but there is no index on the EMP_CLASS.

 

SELECT ENAME

FROM EMP

WHERE EMP_TYPE = 'A'

AND EMP_CLASS = 'X ';

 

The optimizer will notice the index on EMP_TYPE and use it. this is currently the only option. if another non-uniqueness is created on EMP_CLASS after a period of time, the optimizer must select two indexes. In general, the optimizer uses two indexes and performs sorting and merging on their result sets. however, if one index (EMP_TYPE) is close to uniqueness and another index (EMP_CLASS) has thousands of duplicate values. sorting and merging will become an unnecessary burden. in this case, you want the optimizer to block the EMP_CLASS index.

Use the following solution to solve the problem.

SELECT ENAME

FROM EMP

WHERE EMP_TYPE = 'A'

AND EMP_CLASS|''= 'X ';

 

32.Avoid using computation on index columns.

In the WHERE clause, if the index column is part of the function, the optimizer will use full table scan without using the index.

 

Example:

 

Inefficiency:

SELECT...

FROM DEPT

Where sal * 12> 25000;

 

Efficient:

SELECT...

FROM DEPT

Where sal> 25000/12;

 

Press:

This is a very practical rule, please remember

 

33.Automatic index selection

If the table has more than two (including two) indexes, one of them is a unique index, and the other is non-unique.

In this case, ORACLE will use the unique index instead of the unique index.

 

Example:

SELECT ENAME

FROM EMP

Where empno = 2326

And deptno = 20;

 

Here, only the index on EMPNO is unique, so the index on EMPNO is used to retrieve records.

TABLE ACCESS BY ROWID ON EMP

Index unique scan on EMP_NO_IDX

 

34.Avoid using NOT in index Columns

In general, we should avoid using NOT in the index column, NOT will generate the same function as using the index column.

Impact. When ORACLE Encounters "NOT", it will stop using indexes and then perform full table scan.

Example:

 

Inefficient: (here, no index is used)

 

SELECT...

FROM DEPT

WHERE DEPT_CODE NOT = 0;

Efficient: (here, the index is used)

 

SELECT...

FROM DEPT

WHERE DEPT_CODE> 0;

 

Note that,In some casesThe optimizer automatically changes NOTConvert to corresponding Relational operators.

NOT> to <=

NOT> = to <

NOT <to> =

NOT <= to>

 

 

Press:

In this example,The author made some mistakes.Inefficient SQL statements in the exampleCannot be executed.

I did some tests:

SQL> select * from emp where NOT empno> 1;

No rows selected

Execution Plan

----------------------------------------------------------

0 select statement Optimizer = CHOOSE

1 0 table access (by index rowid) OF 'emp'

2 1 INDEX (range scan) OF 'empno' (UNIQUE)

 

SQL> select * from emp where empno <= 1;

No rows selected

Execution Plan

----------------------------------------------------------

0 select statement Optimizer = CHOOSE

1 0 table access (by index rowid) OF 'emp'

2 1 INDEX (range scan) OF 'empno' (UNIQUE)

 

The efficiency of the two is the same, maybe it is in line with the author's"In some casesThe optimizer automatically changes NOTConvert to corresponding Relational operators".

35.Use> =Alternative>

 

If DEPTNO has an index,

 

Efficient:

 

SELECT *

FROM EMP

Where deptno> = 4

Inefficiency:

 

SELECT *

FROM EMP

Where deptno> 3

 

The difference between the two lies in that the former DBMS will jump directly to the first record whose DEPT is equal to 4, while the latter will first locate the record whose DEPTNO is = 3 and scan forward to the record whose first DEPT is greater than 3.

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.