开发者

Hint oracle to use indexes on the subquery -- Oracle SQl

开发者 https://www.devze.com 2023-03-29 07:50 出处:网络
I have a query as follows select * from ( select id,sum(amt) amt from table_t group by id ) t inner join table_v v on (v.id = t.id)

I have a query as follows

select *
from
( select id,sum(amt) amt from table_t group by id
) t inner join table_v v on (v.id = t.id)
order by t.amt desc;

table_t has no index and has 738,000 rows and table_v has an index on id and has 158,000 rows.

The query currently fetches the results in 10 seconds.

The explain query plan shows a full table scan.. How can I improve the performance here ?

If I add an index on id for table_t will it help. Because I am using it in a subque开发者_如何转开发ry ?


If you have an index on (id,amt) you would minimise the work in the group by/summation process (as it could read the index). If both columns are nullable then you may need to add a "where id is not null" so it will use the index. [That's implied by the later join on id, but may not get inferred by the optimizer.]

Next step would be to use a materialized view for the summation, maybe with an index on (amt,id) (which it could use to avoid the sort). But that is refreshed either at a commit or on request or at scheduled intervals. It doesn't help if you need to do this query as part of a transaction.

Both the index and the materialized view would add work to inserts/updates/deletes on the table but save work in this query.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号