Using a function on an indexed column in the WHERE clause will force the optimizer to bypass the desired index (does not apply to function-based indexes).
Common functions used in the WHERE clause are TRUNC, SUBSTR, TO_DATE, TO_CHAR, and INSTR.
The one I am guilty of is using TRUNC on a date column, as in (assuming there is an index on insert_date):
WHERE TRUNC(insert_date) = TRUNC(sysdate);
instead use something like:
WHERE insert_date >= TRUNC(sysdate) and insert_date <>
By taking the function TRUNC off of the indexed column, insert_date, the index on insert_date will be used.
Example:
SQL> set timing on
On our first run the data has not been cached and it take 5 seconds.
SQL> select count(last_updated) "Total" from barcodes where trunc(last_updated) > trunc(sysdate-60);
Total
-------------------
2114
Elapsed: 00:00:05.10
Same SQL but the data now in cache it takes 2.5 seconds.
SQL> select count(last_updated) "Total" from barcodes where trunc(last_updated) > trunc(sysdate-60);
Total
-------------------
2114
Elapsed: 00:00:02.53
Rewrite query to use the index on last_updated and the query takes 0.0 seconds.
SQL> select count(last_updated) "Total" from barcodes where last_updated > trunc(sysdate)-60 and last_updated <>
Total
-------------------
2114
Elapsed: 00:00:00.00
You can't always assume you will use an index just becuase it is available.
Remember your dreams
Remember your dreams
Showing posts with label Indexing. Show all posts
Showing posts with label Indexing. Show all posts
Tuesday, February 3, 2009
Subscribe to:
Posts (Atom)
Proactive Oracle DBA
This is a series of posts. I am working this to share some of the many scripts I schedule to automatically run to alert me of any current o...
-
Oracle 12.1.0.2.0 RMAN-04006: error from auxiliary database: ORA-12514: TNS:listener does not currently know of service requested i...
-
Moving your Oracle database instance to a to new domain? I recently moved several Oracle database servers to a new physical location. We cha...
-
I am using 11g, but this work in 10g as well. I had this annoying alert appearing under my alert tab on OEM for some time. A failed job, y...