oracle need to be careful of the WHERE clause

Some SELECT statement WHERE clause does not use an index. Here are some examples.

In the example below, the '! =' Will not use the index. Remember, the index can only tell you what exists in the table, but can not tell you what does not exist in the table.

Do not use indexes:

SELECT ACCOUNT_NAME

FROM TRANSACTION

WHERE AMOUNT !=0;

Using the index:

SELECT ACCOUNT_NAME

FROM TRANSACTION

WHERE AMOUNT >0;

The following examples, '||' is hyphenation functions. Like other functions as disabled index.

Do not use indexes:

SELECT ACCOUNT_NAME,AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME||ACCOUNT_TYPE=’AMEXA’;

Using the index:

SELECT ACCOUNT_NAME,AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME = ‘AMEX’

AND  ACCOUNT_TYPE=’ A’;

The following examples, '+' is a mathematical function. Like other mathematical functions as disabled index.

Do not use indexes:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE AMOUNT + 3000 >5000;

Using the index:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE AMOUNT > 2000 ;

The example below, the same index column can not be compared with each other, which will enable a full table scan.

Do not use indexes:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME = NVL(:ACC_NAME,ACCOUNT_NAME);

Using the index:

SELECT ACCOUNT_NAME, AMOUNT

FROM TRANSACTION

WHERE ACCOUNT_NAME LIKE NVL(:ACC_NAME,’%’);

 

If you must use a column index to enable the function , ORACLE new features : function-based index (Function-Based Index) might be a better solution .

 CREATE INDEX EMP_I ON EMP (UPPER ( ename)); / * index based function * /

 * The FROM EMP the WHERE the UPPER the SELECT (ename) = 'BLACKSNAIL'; / * index is used * /

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11125553.html