-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify OR/Union type IndexMerge code paths to expand the applicable scenarios of existing capabilities #58361
Comments
very appreciate @time-and-fate could summarize a detailed list about what/how/why of current code base is from several previous history commits which require familiarity. The refactoring means much more than the a feature request itself which needs us to apply the enhancement in #51778 to non-MV indexes. |
I guess it is |
Haha, you are right. The |
Background & Problem
During the development of several previous features, the logic of generating OR/Union type IndexMerge paths has diverged into several different code paths:
generateIndexMerge4NormalIndex()
.... AND (... OR ...) AND ...
, it can consider the filters connected byAND
so the user doesn't need to expand the expression manually to match the index.generateIndexMerge4MVIndex()
generateIndexMerge4ComposedIndex()
.Links to some previous features
json_memberof/overlaps/contains
to IndexMerge to access MVIndex #40191Apparently, it causes code repetitions. If you go over the code, you'll find there are many similar code snippets and there are also many structural similarities.
Besides, for some later enhancements, we introduced them to only 1 or 2 code paths. They can't benefit the other code paths.
For example, for the non-MV index, sometimes the user needs to expand the
... AND (... OR ...)
filter manually to match indexes so that the optimizer can choose an IndexMerge plan.Another example:
The non-MV index logic can consider and try to satisfy the required Sort property, but the MV index logic can't. So sometimes it will need an extra
Sort
. The performance difference will be more obvious when there's alsoLIMIT
in the SQL.Design
Recently, we got a feature request that needs us to apply the enhancement in #51778 to non-MV indexes.
This time, we want to unify the 3 code paths and make the fundamental capabilities available for all scenarios, and of course, reduce the code repetition.
Current implementation
A key data structure here is the
AccessPath
. When anAccessPath
represents an IndexMerge path, there are two cases:AccessPath.PartialIndexPaths []*AccessPath
is used, each element in this slice directly corresponds to a "partial path" in the final plan. Code paths 2 and 3 use this.AccessPath.PartialAlternativeIndexPaths [][]*AccessPath
is used, all possible alternatives for a "partial path" are stored. Then it can try to choose the one that satisfies the Sort property infindBestTask()
(the required Sort property is only available at that time). Code path 1 uses this.Using the
AccessPath
as the dividing line, we can roughly divide the overall process of generating OR type IndexMerge path into two steps:generateIndexMergePath()
, it will match the filters against candidate indexes, try to find possible IndexMerge paths, and store them in theAccessPath
.... AND (... OR ...) AND ...
example above, it won't try to use other filters connected by AND.findBestTask()
:AccessPath.PartialIndexPaths
case, it just directly builds into the final plan.AccessPath.PartialAlternativeIndexPaths
case, there is an extra step (inmatchPropForIndexMergeAlternatives()
) that chooses the alternatives that satisfy the Sort property, then builds into a normalAccessPath
withAccessPath.PartialIndexPaths
set.Changes
[]*AccessPath
json_overlaps(j, '[1,2]')
onindex(cast j to unsigned array)
will become two paths.AccessPath.PartialAlternativeIndexPaths
is the most obvious one. It will be changed from[][]*AccessPath
to[][][]*AccessPath
.findBestTask() -> matchPropForIndexMergeAlternatives()
, it will need to be able to handle MV indexes.*AccessPath
.AccessPath
, i.e., switching fromAccessPath.PartialIndexPaths
toAccessPath.PartialAlternativeIndexPaths
.matchPropForIndexMergeAlternatives()
.CalcTotalSelectivityForMVIdxPath()
for estimation, but the non-MV indexes case usesSelectivity()
.The text was updated successfully, but these errors were encountered: