1

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 subquery ?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.