May 25, 2010
The book “Expert One-On-One Oracle” has been on the edge of my desk for the last couple of days. I cannot recall why I originally pulled it off the bookshelf, but possibly it was needed to satisfy a question about how things used to work in Oracle Database. I opened the book to a random page, and ended up somewhere around page 303. Page 303 includes a section title of “Why isn’t my Index Getting Used?”. This still seems to be a frequent question on various Oracle forums, and volumes have been written on the subject. A quick Google search finds the following on the topic:
The “Expert One-On-One Oracle” book lists 6 of the cases why an index would not be used. Some things in Oracle stay the same from one release to the next, while others change. Which of the following are true or false for Oracle Database 184.108.40.206 or higher, and indicate why the answer is true or false, and if relevant, the Oracle release when the answer changed. Note that I might intentionally state only part of the case identified in the book, in part to make things more interesting. Maybe the question of when is it true and when is it not true would be a more realistic request?
Reasons why an index would not be used:
1. The predicate in the WHERE clause does not specify the column at the leading edge of the index. For example, an index exists on T1(C1, C2) and the WHERE clause in the query is simply WHERE C2=1.
2. The query is attempting to count the number of rows in the table, for example: SELECT COUNT(*) FROM T1;.
3. The query is attempting to apply a function on the indexed columns, for example: SELECT * FROM T1 WHERE TRUNC(DATE_COL) = TRUNC(SYSDATE);.
4. The query is attempting to treat a VARCHAR2/CHAR column that contains only numbers as a numeric column, for example: SELECT * FROM T1 WHERE CHAR_COL = 10;.
5. The index, if used, would be slower, as determined by the calculated cost of the access path.
6. The tables have not been analyzed in a while. The last time the tables referenced in the WHERE clause were analyzed, the tables were quite small. Now the tables are significantly larger. Without up-to-date statistics, the CBO cannot make the correct decisions.
Extra credit: list three other reasons why an index might not be used.
(10053 trace for second test of table T1 in the comments section: OR11_ora_4400_WhyFullTableScanTest - Edit: this is a PDF file, WordPress is not allowing the double extention trick nor Word formatted files for some reason.)