There is a lot to be said about when to add indexes on Oracle tables, but I wanted to show a simple example. In this case we have a table with no indexes and a query with two equal conditions in the where clause which identify a single row out of many. Here is a zip of the SQL*Plus script and log for this post: zip
The table TEST is a clone of DBA_TABLES. I load it up with a bunch of copies of DBA_TABLES and with a single row that is unique. Then I run the query without any indexes on the table:
SQL> select TABLESPACE_NAME
2 from test
3 where
4 owner='TESTOWNER' and
5 table_name = 'TESTTABLE';
TABLESPACE_NAME
------------------------------
TESTTABLESPACE
Elapsed: 00:00:00.15
I add an index on owner and table_name and run it again:
SQL> select TABLESPACE_NAME
2 from test
3 where
4 owner='TESTOWNER' and
5 table_name = 'TESTTABLE';
TABLESPACE_NAME
------------------------------
TESTTABLESPACE
Elapsed: 00:00:00.00
This may not seem like a big deal going from .15 seconds to .00 seconds (less than .01 seconds). But if you start running a query like this hundreds of thousands of times per hour the .15 seconds of CPU per execution can slow your system down.
See the zip for the details. The create index command looked like this:
SQL> create index testi on test (owner, table_name);
Index created.
Bobby
Pingback: Real World Index Example | Bobby Durrett's DBA Blog