I have a table with more than 10 million rows, and I use simple queries with bind variables (in my case I can't search by primary key id).
The table looks like this:
Col1 Col2 Col3 Col4 Col5 Col6
and the queries is like
select distinct col1
from table ;
select distinct col2
where col1 = :bind ;
select distinct col3
where col1 = :bind1 and col2 = :bind2 ;
.
.
select distinct col6
where col1 = :bind1 and col2 = :bind2 and col3 = :bind3
and col4 = :bind4 and col5 = :bind5
The results of all of these queries are not large - less than 100 records at MAX - but the performance is too slow .
How to improve it ?
You can add an index on (col1, col2, col3, col4, col5)
. This can be used for all the queries:
create index idx_t_5 on t(col1, col2, col3, col4, col5);
If that is not possible and the columns have the same type, then you can use a lateral join in Oracle 12c+ to combine this into a single query:
select distinct which, col
from t cross apply
(select 'col1' as which, t.col1 as col from dual union all
select 'col2', t.col2 from dual where t.col2 = :bind2 union all
. . .
) t
where col1 = :bind1;
This will scan the table only once, which be a performance improvement.
ok , but if want to select 2 columns how will the syntax be ?
its really complicated could you elaborate please ?
@osfar While it's a bit more complicated that your typical select, it's quite useful. Yet, you really, really need to get the right indexes in place. A correct index can see speed improvements of hundreds of times. In the past I saw creating an index on a 10M+ table reduce query time from over 1 hour to under 1 second.
@osfar . . . If you wanted to select two columns, you would have a different question. But you can easily add more columns into the lateral join.
@ Aleks G how to make the perfect index for my case ?