I am having trouble trying to re-write this query to avoid a full index scan:
select *
from (
select asxsp.*
,row_number() over (partition by fk_asxsc_id order by last_price_date desc) rn
from asx_share_price asxsp
) iq
where rn = 1
and iq.fk_asxsc_id in (select max(id) from asx_share_company)
Explain plan:
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=28487 Card=3027112
Bytes=227033400)
1 0 VIEW (Cost=28485 Card=3027112 Bytes=227033400)
2 1 WINDOW (SORT PUSHED RANK) (Cost=28485 Card=3027112 Bytes
=87786248)
3 2 INDEX (FAST FULL SCAN) OF 'ASXSP_PK' (INDEX (UNIQUE))
(Cost=4097 Card=3027112 Bytes=87786248)
4 1 SORT (AGGREGATE)
5 4 INDEX (FULL SCAN (MIN/MAX)) OF 'ASXSC_PK' (INDEX (UNIQ
UE)) (Cost=2 Card=3585 Bytes=14340)
Sorry if it is hard to read I dont know how to format it properly.
If the query is written as :
select *
from (
select asxsp.*
,row_number() over (partition by fk_asxsc_id order by last_price_date desc) rn
from asx_share_price asxsp
) iq
where rn = 1
and iq.fk_asxsc_id in (23050)
I get the desired plan:
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=5616 Card=396077 B
ytes=29705775)
1 0 VIEW (Cost=5616 Card=396077 Bytes=29705775)
2 1 WINDOW (SORT PUSHED RANK) (Cost=5616 Card=396077 Bytes=1
1486233)
3 2 INDEX (RANGE SCAN) OF 'ASXSP_PK' (INDEX (UNIQUE)) (Cos
t=2429 Card=396077 Bytes=11486233)
Can anyone shed some light on how I can avoid the full index scan?