2

Given a table with columns A, B, and C of type CHAR(15) and an unique index

If I have a query with a WHERE clause specifying A and C, oracle's optimizer chooses to do an index range scan. But if I know that the cardinality of B is low, should I hint oracle to use an index skip scan? Will a skip scan be more performant skipping a non-leading column (in this case, B) than a range scan? Will the skip scan skip both A and B in the index or will it just skip B?

Tony Meng
  • 353
  • 4
  • 12
  • I think this question is better addressed at http://dba.stackexchange.com/. At least I would address such a question to my DBA. – alko Nov 19 '13 at 23:01
  • This question appears to be off-topic because it is about DBA duties and has been cross-posted on DBA.StackExchange. – Andrew Barber Nov 20 '13 at 02:10

1 Answers1

2

In general, the optimizer is much better at figuring out whether a range scan would be more efficient than a skip scan (or vice versa). If the optimizer is making the wrong choice, the problem almost always comes down to the human not providing the optimizer with the proper information in the form of accurate statistics on the table and on the indexes (including things like histograms on columns that contain skewed data).

For any given query, the only way to know with certainty which of two plans is more efficient is to try it out with your data on your hardware with your query. You can try adding the hint to force the plan to change and you can then measure which approach is actually more efficient. If the plan the optimizer is choosing isn't the most efficient, I would tend to suspect that the root cause is that some aspect of the statistics are not correct but that is something that you'd need to look at a 10053 trace to get to the bottom of in most cases. Rarely, there are cases where the optimizer is wrong because the optimizer isn't able to take into account some aspect of the data (for example, until recently, it was unable to deal with situations where the data in multiple columns was not independent such as a car table with make, model, and year columns where knowing one value constrained the set of valid values in the other columns).

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • 1
    I think the question is rather if a skip scan is even applicable in this case. Can a skip scan be used to "skip" a non leading column or can it only the leading one? – Joel Palmert Nov 19 '13 at 23:19
  • @Joel, good question. On my 11gR1 instance I can force an index skip scan in this scenario (where the leading column in the index has an equality predicate). Mind you, I'm not sure how often this will be chosen by the optimiser by itself. – Jeffrey Kemp Nov 20 '13 at 05:59
  • create table jktest (a number, b number, c number, d number); create index jktesti on jktest (a, b, c);select /*+INDEX_SS(jktest jktesti)*/ * from jktest where a = :1 and c = :2; select /*+INDEX_SS(jktest jktesti)*/ * from jktest where a = :1; – Jeffrey Kemp Nov 20 '13 at 06:00
  • Ye, I also got it to show up as skip scan in the explain plan by hint but I was unsure if that meant it skipped the second column or if it skipped the first (even though it was specified). – Joel Palmert Nov 21 '13 at 01:01