Question

I have a table_A (C1,C2,C3,.....,Cn, datestamp)

Note : Column datestamp is indexed

QRY1

select * from 
(select max(datestamp) dates from table_A) t,
table_A
where a.datestamp = t.dates

QRY2

   select * from (
   select a.* , max (datestamp) over() dates from table_A a))
   where datestamp = dates

Explain Paln of QRY1 using indexed scan (execyted in 1 sec) cost was very less.

But for QRY2 going for full table scan (execyted in 8 sec) cost was very high.

Just wanted to know why analytical function ignores indexes.

Thanks (I am usig PL/SQL Oracle 10g)

Was it helpful?

Solution

From the documentation:

Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group.
...
Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed.

Your inner select is pulling all rows from your table because it has no where clause; since it has to fetch every row the index isn't helpful and it might as well do a full table scan. Using the index on datestamp would be worse - it would have to do a full scan of the index and then access every data row anyway, increasing the IO. If you run the inner select on its own you'll see it returns a lot of rows, not just one.

The outer select then matches a specific row (or rows) from the inner one. That comparison can't be pushed into the inner select because of when in the processing the analytic value is calculated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top