My question is about Oracle 11g and the use of indexes in SQL queries.
There is a table in my database that is structured as follows:
Table tab ( rowid NUMBER(11), unique_id_string VARCHAR2(2000), year NUMBER(4), dynamic_col_1 NUMBER(11), dynamic_col_1_text NVARCHAR2(2000) ) TABLESPACE tabspace_data;
I created two indexes:
CREATE INDEX Index_dyn_col1 ON tab (dynamic_col_1, dynamic_col_1_text) TABLESPACE tabspace_index; CREATE INDEX Index_unique_id_year ON tab (unique_id_string, year) TABLESPACE tabspace_index;
The table contains from 1 to 2 million records. I am extracting data from it by executing the following SQL command:
SELECT distinct "sub_select"."dynamic_col_1" "AS_dynamic_col_1","sub_select"."dynamic_col_1_text" "AS_dynamic_col_1_text" FROM ( SELECT "tab".* FROM "tab" where "tab".year = 2011 ) "sub_select"
Unfortunately, it takes about 1 hour to complete the request, although I created both indexes described above. The explanation plan shows that Oracle uses "Full table access", that is, a full table scan. Why is the index not used?
As an experiment, I tested the following SQL command:
SELECT DISTINCT "dynamic_col_1" "AS_dynamic_col_1", "dynamic_col_1_text" "AS_dynamic_col_1_text" FROM "tab"
Even in this case, the index is not used and a full table scan is performed.
In my real database, the table contains more indexed columns such as "dynamic_col_1" and "dynamic_col_1_text". The entire index file is about 50 GB in size.
Some additional information:
- The Oracle 11g database is installed on my local computer.
- I am using Windows 7 Enterprise 64bit.
- The entire index is divided into 3 dbf files about 50 GB in size.
I would really be happy if someone could tell me how to get Oracle to use the index in the first query. Since the first query is used by another program to retrieve data from the database, it can hardly be changed. Therefore, it would be nice to set up the table.
Thanks in advance.
[01.10.2011: UPDATE]
I think I found a solution to the problem. Both dynamic_col_1 and dynamic_col_1_text columns are NULL. After modifying the table to ban NULL values ββin both columns and adding a new index exclusively for the year column, Oracle performs a quick index scan. The advantage is that the request takes about 5 seconds to complete, and not 1 hour, as before.