web analytics

When Indexes Aren't Used in the Database?

Options

codeling 1602 - 6666
@2016-01-28 14:49:04

NULL values cause indexes to be suppressed

When you use IS NULL or IS NOT NULL in the WHERE clauses in your SQL statement, index usage will be suppressed, since the value of NULL is undefined. There is no value in the database that will equal a NULL value; not even NULL equals a NULL.

NULL values pose several difficulties for SQL statements. Indexed columns that have rows containing a NULL value will not have an entry in the index (except for bitmapped indexes—which is why bitmap indexes are usually fast for NULL searches). Under normal circumstances the following statement would cause a full table scan to be performed, even if the Sal column is indexed:

select empno, ename, deptno
from emp
where sal is null;

To disallow NULL values for the columns, use NOT NULL when creating or altering the table. Note that if the table already contains data, you can only set a column to NOT NULL if it has a non-NULL value for every row or if you use the DEFAULT clause of the ALTER TABLE command.

The following listing shows the modification of the EMP table’s Sal column to disallow NULL values:

alter table emp modify
(sal not null);

Note that an error will be returned if insertion of a NULL value is attempted for the Sal column.

@2016-01-29 08:45:20

Using Functions in WHERE Clause May Cause Indexes To Be Suppressed

Unless you are using function-based indexes, using functions on indexed columns in the WHERE clause of a SQL statement will cause the database optimizer to bypass indexes. Some of the most common functions in Oracle are TRUNC, SUBSTR, TO_DATE, TO_CHAR, and INSTR. All of these functions will cause the value of the column to be altered. Therefore, the indexes and the columns being referenced will not be used.

The following statement would cause a full table scan to be performed, even if there is an index on the hire_date column (as long as it wasn’t a function-based index):

select empno, ename, deptno
from emp
where trunc(hiredate) = '01-MAY-01';

Changing the statement to the following would allow for an index lookup:

select empno, ename, deptno
from emp
where hiredate > '01-MAY-01'
and hiredate < (TO_DATE('01-MAY-01') + 0.99999);

By altering the values being compared to the column, and not the columns themselves, the indexes become available. This is used to eliminate full table scans.

@2016-01-31 10:22:18

Using the NOT EQUAL Operators in WHERE Clause Cause Indexes To Be Suppressed

Indexes can only be used to find data that exists within a table. Whenever the not equal operators ( ‘<>’, ‘!=’) are used in the WHERE clause, indexes on the columns being referenced cannot be used.

Consider the following query on the CUSTOMERS table, which has an index on the CUST_RATING column. The following statement would result in a full table scan (since most records would usually be retrieved) even though there is an index on the CUST_RATING column:

select cust_id, cust_name
from customers
where cust_rating <> 'aa';

When you analyze your tables, Oracle collects statistics about the distribution of data within the table. Using that analysis, the cost-based optimizer may decide to use the index for some values in your WHERE clause but not for other values. During application development and testing you should use a representative set of rows so that you can simulate the actual distribution of data values in the production environment.

@2016-01-31 10:32:18

Nice to know.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com