How to force oracle to use index range scan?

前端 未结 5 1662
执笔经年
执笔经年 2021-02-04 08:10

I have a series of extremely similar queries that I run against a table of 1.4 billion records (with indexes), the only problem is that at least 10% of those queries take > 100x

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 08:43

    I suggest the following approach:-

    • Get an explain plan on the slow statement
    • Using an INDEX hint, get an explain plan on using the index

    You'll notice that the cost of the INDEX plan is greater. This is why Oracle is not choosing the index plan. The cost is Oracle's estimate based on the statistics it has and various assumptions.

    If the estimated cost of a plan is greater, but it actually runs quicker then the estimate is wrong. Your job is to figure out why the estimate is wrong and correct that. Then Oracle will choose the right plan for this statement and others on it's own.

    To figure out why it's wrong, look at the number of expected rows in the plan. You will probably find one of these is an order of magnitude out. This might be due to non-uniformly distributed column values, old statistics, columns that corelate with each other etc.

    To resolve this, you can get Oracle to collect better statistics and hint it with better starting assumptions. Then it will estimate accurate costs and come up with the fastest plan.

    If you post more information I might be able to comment further.

提交回复
热议问题