Anonymous,

Thanks for the comment.

I think you may be using the word popular in two different ways here.

To the CBO “popular” means “a value that appears in a large number of rows”.

To your application “popular” seems to mean “this is a value that people are often querying”.

It’s has always been a common problem that the “interesting” data (from the users’ persective) and the “average” data when the CBO crunches the numbers look very different leading to execution plans that are sensible for the average case but bad from the users’ perspective.

It’s a little hard to imagine exactly what your data might look like – and I do wonder if increasing the number of buckets from the default 254 to the maximum, and setting an explicit larger sample size for the histogram might help. But maybe you have something like this:

a) a small number of VERY frequently used values

b) a larger number of frequently used values

c) a small number of rare values – which are the values that users likely to query.

** “small” and “larger” are very vague and flexible terms in this argument

With a little bad luck you may have a histogram that captures all of category (a) and some of category (b), but misses a lot of category (b) – and category (c) doesn’t have a hope of being captured ever, they’ll just be hidden in the buckets somewhere.

In this case the arithmetic would end up with one number for the average number of rows for the category (c) and missed category (b) – and maybe that’s quite a large number fairly close to the frequency (or half the frequency) of the average category (b) – hence the tablescan rather than index access.

More buckets would increase the possibly precision.

A higher sample size might increase the number of distinct values found, hence reduce the result of the “average non-popular frequency” calculation.

Ultimately you may simply have to fake a histogram that gives Oracle enough information to get a good estimate of the rare values that the users like. (e.g. fake a fequency histogram – https://jonathanlewis.wordpress.com/2018/10/15/faking-histograms/

Regards

Jonathan Lewis