Oracle SQL Performance Optimization series (13)

Source: Internet
Author: User

43.Use WHEREReplace ORDER

The order by clause only uses indexes under two strict conditions.

 

All columns in order by must be included in the same index and maintained in the ORDER of the index.

All columns in order by must be defined as non-empty.

 

The index used BY the WHERE clause and the index used in the order by clause cannot be tied together.

 

For example:

The DEPT table contains the following columns:

 

DEPT_CODE PK NOT NULL

DEPT_DESC NOT NULL

DEPT_TYPE NULL

Non-unique index (DEPT_TYPE)

 

Inefficiency: (indexes are not used)

SELECT DEPT_CODE

FROM DEPT

Order by DEPT_TYPE

 

Explain plan:

SORT ORDER

TABLE ACCESS FULL

 

Efficiency: (using indexes)

 

SELECT DEPT_CODE

FROM DEPT

WHERE DEPT_TYPE> 0

Explain plan:

TABLE ACCESS BY ROWID ON EMP

Index range scan on DEPT_IDX

Press:

ORDERYou can also use indexes!This is indeed an easily overlooked knowledge point.Let's verify:

SQL> select * from emp order by empno;

Execution Plan

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

0 select statement Optimizer = CHOOSE

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

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

 

44.Avoid changing the index column type.

ORACLE automatically converts columns to different types of data.

 

Assume that EMPNO is a numeric index column.

 

SELECT...

FROM EMP

Where empno = '20140901'

 

In fact, after ORACLE type conversion, the statement is converted:

SELECT...

FROM EMP

Where empno = TO_NUMBER ('20140901 ')

 

Fortunately, the type conversion does not occur on the index column, and the purpose of the index is not changed.

 

Assume that EMP_TYPE is a character-type index column.

SELECT...

FROM EMP

WHERE e-mapreduce type = 123

 

This statement is converted:

SELECT...

FROM EMP

WHERE TO_NUMBER (EMP_TYPE) = 123

 

This index will not be used because of internal type conversion!

Press:

To avoid ORACLEFor your SQLImplicit type conversion,It is best to explicitly display the type conversion.Note that when comparing characters and valuesConverts the value type to the character type first.

 

45.WHERE to be carefulClause

The WHERE clause in some SELECT statements does not use indexes. Here are some examples.

In the example below ,'! = 'No index will be used. Remember, indexes only tell you what exists in the table, but not what does not exist in the table.

No index is used:

SELECT ACCOUNT_NAME

FROM TRANSACTION

Where amount! = 0;

Use index:

SELECT ACCOUNT_NAME

FROM TRANSACTION

Where amount> 0;

 

In the following example, '|' is a character concatenation function. indexes are disabled just like other functions.

No index is used:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME | ACCOUNT_TYPE = 'amexa ';

Use index:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME = 'amex'

AND ACCOUNT_TYPE = 'a ';

 

In the following example, '+' is a mathematical function. As with other mathematical functions, indexes are disabled.

No index is used:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

Where amount + 3000> 5000;

Use index:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

Where amount> 2000;

In the following example, the same index Columns cannot be compared with each other, which enables full table scan.

No index is used:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME = NVL (: ACC_NAME, ACCOUNT_NAME );

Use index:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME like nvl (: ACC_NAME, '% ');

 

Press:

If you must enable the index for columns using the functionNew features:Function-Based Index)It may be a good solution.

Create index EMP_ I ON EMP (UPPER (ename ));/*Create a function-based index */

SELECT * FROM emp where upper (ename) = 'blacksnail ';/*Index will be used */

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.