Using an index degrades query performance if it leads to an increase in the number of disk I / O compared to querying a table with an index.
This can be demonstrated with a simple table:
create table tq84_ix_test ( a number(15) primary key, b varchar2(20), c number(1) );
The next block fills 1 million records in this table. Each 250th record is filled with the rare value symbol in column b, and all the rest are filled with frequent value :
declare rows_inserted number := 0; begin while rows_inserted < 1000000 loop if mod(rows_inserted, 250) = 0 then insert into tq84_ix_test values ( -1 * rows_inserted, 'rare value', 1); rows_inserted := rows_inserted + 1; else begin insert into tq84_ix_test values ( trunc(dbms_random.value(1, 1e15)), 'frequent value', trunc(dbms_random.value(0,2)) ); rows_inserted := rows_inserted + 1; exception when dup_val_on_index then null; end; end if; end loop; end; /
Index is placed in a column
create index tq84_index on tq84_ix_test (b);
The same query, but once with an index and once without an index, is different in performance. Check it out for yourself:
set timing on select sum(c) from tq84_ix_test where b = 'frequent value'; select sum(c) from tq84_ix_test where b = 'frequent value';
Why? In the case without an index, all database blocks are read in sequential order. This is usually expensive and therefore considered bad. In the usual situation with the index, such a “full table scan” can be reduced to reading, say, from 2 to 5 blocks of the index database, as well as reading one database block containing the record pointed to by the pointer. In this example, this is completely different: the entire index is read, and for (almost) every record in the index, a database block is also read. Thus, not only the entire table is read, but also the index. Note that this behavior will be different if c also in the index, because in this case, Oracle can select the value of c from the index instead of going through the table.
So, to summarize the problem: if the index does not select multiple records, it might be useful not to use it.