Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Index with multiple Columns querying on single column

In a table in our Oracle installation we have a table with an index on two of the columns (X and Y). If I do a query on the table with a where clause only touching column X, will Oracle be able to use the index?

For example:

Table Y: Col_A, Col_B, Col_C,

Index exists on (Col_A, Col_B)

SELECT * FROM Table_Y WHERE Col_A = 'STACKOVERFLOW';

Will the index be used, or will a table scan be done?

like image 511
Ray Booysen Avatar asked Aug 15 '12 14:08

Ray Booysen


People also ask

Can an index contain multiple columns?

An index can be defined on more than one column of a table. For example, if you have a table of this form: CREATE TABLE test2 ( major int, minor int, name varchar );

Can we create multiple index on single column?

A multicolumn index should be created using the columns specified as the search condition then the columns to be grouped or sorted, in this order. In this case also, a multicolumn index consisting of columns C1, C2, C3, and C4 should be created, instead of creating two single-column indexes in columns C1 and C2.

Can we create multiple index on same column in Oracle?

For example, if you have an index on column {a} or columns {a,b}, you can't create another index on the same column or set of columns in the same order. In 12c, you can have multiple indexes on the same column or set of columns as long as the index type is different.

Can Oracle use multiple indexes at once on a single table?

Oracle 12c allows multiple indexes on the same set of columns, provided only one index is visible and all indexes are different in some way.


1 Answers

It depends.

You could check it by letting Oracle explain the execution plan:

EXPLAIN PLAN FOR 
   SELECT * FROM Table_Y WHERE Col_A = 'STACKOVERFLOW';

and then

select * from table(dbms_xplan.display);

So, for example with

create table table_y (
  col_a varchar2(30),
  col_b varchar2(30),
  col_c varchar2(30)
);

create unique index table_y_ix on table_y (col_a, col_b);

and then a

explain plan for
  select * from table_y
  where col_a = 'STACKOVERFLOW';

select * from table(dbms_xplan.display);

The plan (on my installation) looks like:

------------------------------------------------------------------------------------------
| Id  | Operation                   | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |            |     1 |    51 |     1   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TABLE_Y    |     1 |    51 |     1   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | TABLE_Y_IX |     1 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("COL_A"='STACKOVERFLOW')

ID 2 shows you, that the index TABLE_Y_IX is indeed used for an index range scan.

If on another installation Oracle chooses to use the index is dependend on many things. It's Oracle's query optimizer that makes this decision.

Update If you feel you're be better off (performance wise, that is) if Oracle used the index, you might want to try the + index_asc(...) (see index hint)

So in your case that would be something like

SELECT /*+ index_asc(TABLE_Y TABLE_Y_IX) */ * 
  FROM Table_Y 
 WHERE Col_A = 'STACKOVERFLOW';

Additionally, I would ensure that you have gathered statistics on the table and its columns. You can check the date of the last gathering of statistics with a

select last_analyzed from dba_tables where table_name = 'TABLE_Y';

and

select column_name, last_analyzed from dba_tab_columns where table_name = 'TABLE_Y';

If there are no statistics or if they're stale, make yourself familiar with the dbms_stats package to gather such statistics.

These statistics are the data that the query optimizer relies on heavily to make its decisions.

like image 99
René Nyffenegger Avatar answered Nov 15 '22 15:11

René Nyffenegger