Skip to content
Advertisement

Strange behavior of Oracle and group by

While analyzing performance of an SQL query in Oracle, I noticed a strange behavior. I noticed that Oracle’s plan behavior changes depending on value used in query.

For example here is my table structure:

When I fetched plan of following query, I see that table is accessed by INDEX RANGE SCAN, which is expected:

However, when I change the value only, I noticed that table is accessed by TABLE ACCESS FULL, which is very strange for me:

My question is, why does it happen? I would expect Oracle to use INDEX RANGE SCAN, no matter what the value is.

My database is Oracle 11g

Advertisement

Answer

the optimizer may decide whether to use or not to use an individual index depending on the amount of the data, for huge set of data the full-scan is preferred rather than the index range scan.

Your second case seems scanning a bigger data set as being the interval is longer.

As an example, try to restrict your scans for only one-month period

Q1 :

and

Q2 :

for both queries Q1 and Q2, you most probably can see a index range scan with close values of costs depending on the homogeneously populated data for the table. The indexes are mostly good for small number of rows.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement