-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
plan, stats: fix inconsistent row count estimation #7233
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,8 @@ type exprSet struct { | |
mask int64 | ||
// ranges contains all the ranges we got. | ||
ranges []*ranger.Range | ||
// numCols is the number of columns contained in the index or column(which is always 1). | ||
numCols int | ||
} | ||
|
||
// The type of the exprSet. | ||
|
@@ -177,7 +179,7 @@ func (coll *HistColl) Selectivity(ctx sessionctx.Context, exprs []expression.Exp | |
if err != nil { | ||
return 0, errors.Trace(err) | ||
} | ||
sets = append(sets, &exprSet{tp: colType, ID: col.ID, mask: maskCovered, ranges: ranges}) | ||
sets = append(sets, &exprSet{tp: colType, ID: col.ID, mask: maskCovered, ranges: ranges, numCols: 1}) | ||
if mysql.HasPriKeyFlag(colInfo.Info.Flag) { | ||
sets[len(sets)-1].tp = pkType | ||
} | ||
|
@@ -190,7 +192,7 @@ func (coll *HistColl) Selectivity(ctx sessionctx.Context, exprs []expression.Exp | |
if err != nil { | ||
return 0, errors.Trace(err) | ||
} | ||
sets = append(sets, &exprSet{tp: indexType, ID: idxInfo.ID, mask: maskCovered, ranges: ranges}) | ||
sets = append(sets, &exprSet{tp: indexType, ID: idxInfo.ID, mask: maskCovered, ranges: ranges, numCols: len(idxInfo.Info.Columns)}) | ||
} | ||
} | ||
sets = getUsableSetsByGreedy(sets) | ||
|
@@ -254,16 +256,20 @@ func getUsableSetsByGreedy(sets []*exprSet) (newBlocks []*exprSet) { | |
mask := int64(math.MaxInt64) | ||
for { | ||
// Choose the index that covers most. | ||
bestID, bestCount, bestTp := -1, 0, colType | ||
bestID, bestCount, bestTp, bestNumCols := -1, 0, colType, 0 | ||
for i, set := range sets { | ||
set.mask &= mask | ||
bits := popCount(set.mask) | ||
// This set cannot cover any thing, just skip it. | ||
if bits == 0 { | ||
continue | ||
} | ||
if (bestTp == colType && set.tp < colType) || bestCount < bits { | ||
bestID, bestCount, bestTp = i, bits, set.tp | ||
// We greedy select the stats info based on: | ||
// (1): The stats type, always prefer the primary key or index. | ||
// (2): The number of expression that it covers, the more the better. | ||
// (3): The number of columns that it contains, the less the better. | ||
if (bestTp == colType && set.tp < colType) || bestCount < bits || (bestCount == bits && bestNumCols > set.numCols) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is required by this PR, because after the change in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
bestID, bestCount, bestTp, bestNumCols = i, bits, set.tp, set.numCols | ||
} | ||
} | ||
if bestCount == 0 { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is always 1, why don't use a const?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is always 1 for the column, while it could also greater than 1 for the index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha.