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?
It depends.
You could check it by letting Oracle explain the execution plan:
and then
So, for example with
and then a
The plan (on my installation) looks like:
ID 2 shows you, that the index
TABLE_Y_IXis indeed used for anindex 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
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
and
If there are no statistics or if they’re stale, make yourself familiar with the
dbms_statspackage to gather such statistics.These statistics are the data that the query optimizer relies on heavily to make its decisions.