WL#13929: Introduce new optimizer switch to disable limit optimization

Affects: Server-8.0   —   Status: Complete

The optimization to switch from a non-ordering index to an
ordering index for group by and order by when there is a
limit clause goes very wrong for certain queries. Some of
the problems with the algorithm w.r.t this aspect are:

1.Algorithm assumes that data distribution is uniform.
For ex: If a query qualifies 10 rows out of 100 rows,
it presumes that every 10th row will qualify. However
if all the 10 qualifying rows are not distributed uniformly
and if the algorithm picks a ordering index over ref/range
index, then server ends up reading way too many rows than
its estimation. This results in a bad plan compared to the
original selection made by the optimizer.

2.Algorithm presumes that non-ordering index and the
ordering index being compared are not-correlated.

3.Algorithm is not aware of additional predicates that
could be pushed later using index condition pushdown for
a range/ref access method. This would further result in
reduction of qualifying rows for non-ordering index.
So the estimations for the final rows to be read for an
ordering index is not right resulting in picking ordering
index while the non-ordering index would have performed
much better.

4.The cost model used in the algorithm is flawed in arriving
at index scan time for the ordering index.Also, its
preference to pick covering index even when the ordering index
scan time is no-better and preferring to switch to ordering
index when group by is present without taking into consideration
other parameters results in bad plans for certain queries.

There are also other cases where it does not make the right
decision of choosing ordering index. W.r.t this aspect, there
are problems too.

1.The cost model used in the algorithm is flawed. It does not
take into consideration the external sort cost for a non-ordering
index.

2.Picking of ordering index is done very late. Ideally it should
be part of the join order decision making which could take
advantage of the requested order and also the limit clause.

As seen in the many bugs reported over years, this is an
optimization that can pick good plans but also very bad plans too.

The optimizer switch which is introduced as part of this patch
enables users to disable this optimization if the switch to
ordering index results in bad plan. However the other problem of
not switching to ordering index needs a change in the algorithm
itself and cannot be addressed with the introduction of this switch.

We have tested and analyzed most of the bugs reported in this area
and identified the above problems with the algorithm. We will not
be solving these problems with this change, but giving user an
option to not use the optimization.