-
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: calc access path
when doing deriveStats
.
#6346
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
391e389
plan: calc `index path` when doing `deriveStats`.
winoros 2039b18
Merge branch 'master' into move-index-calc-front
shenli 50fd959
address comments.
winoros ba77bdf
address comment.
winoros d1a7691
Merge branch 'master' into move-index-calc-front
winoros 8192bfa
address comments.
winoros 5d3620f
address comment.
winoros d170ac3
address comments.
winoros e528c54
address comment.
winoros 8d5775b
address comments.
winoros e6bc81b
Merge branch 'master' into move-index-calc-front
zz-jason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,18 @@ | |
package plan | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/ast" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/expression/aggregation" | ||
"github.com/pingcap/tidb/model" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/statistics" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/ranger" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
|
@@ -299,13 +304,85 @@ type DataSource struct { | |
|
||
statisticTable *statistics.Table | ||
|
||
// availableIndices is used for storing result of availableIndices function. | ||
availableIndices *availableIndices | ||
// possibleAccessPaths stores all the possible access path for physical plan, including table scan. | ||
possibleAccessPaths []*accessPath | ||
} | ||
|
||
// accessPath tells how we access one index or just access table. | ||
type accessPath struct { | ||
index *model.IndexInfo | ||
ranges []*ranger.Range | ||
// countAfterAccess is the row count after we apply range seek and before we use other filter to filter data. | ||
countAfterAccess float64 | ||
// countAfterIndex is the row count after we apply filters on index and before we apply the table filters. | ||
countAfterIndex float64 | ||
accessConds []expression.Expression | ||
eqCondCount int | ||
indexFilters []expression.Expression | ||
tableFilters []expression.Expression | ||
// isTablePath indicates whether this path is table path. | ||
isTablePath bool | ||
// forced means this path is generated by `use/force index()`. | ||
forced bool | ||
} | ||
|
||
func (ds *DataSource) deriveTablePathStats(path *accessPath) error { | ||
var err error | ||
sc := ds.ctx.GetSessionVars().StmtCtx | ||
path.countAfterAccess = float64(ds.statisticTable.Count) | ||
path.tableFilters = ds.pushedDownConds | ||
var pkCol *expression.Column | ||
if ds.tableInfo.PKIsHandle { | ||
if pkColInfo := ds.tableInfo.GetPkColInfo(); pkColInfo != nil { | ||
pkCol = expression.ColInfo2Col(ds.schema.Columns, pkColInfo) | ||
} | ||
} | ||
if pkCol == nil { | ||
path.ranges = ranger.FullIntRange(false) | ||
return nil | ||
} | ||
path.ranges = ranger.FullIntRange(mysql.HasUnsignedFlag(pkCol.RetType.Flag)) | ||
if len(ds.pushedDownConds) == 0 { | ||
return nil | ||
} | ||
path.accessConds, path.tableFilters = ranger.DetachCondsForTableRange(ds.ctx, ds.pushedDownConds, pkCol) | ||
path.ranges, err = ranger.BuildTableRange(path.accessConds, sc, pkCol.RetType) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
path.countAfterAccess, err = ds.statisticTable.GetRowCountByIntColumnRanges(sc, pkCol.ID, path.ranges) | ||
return errors.Trace(err) | ||
} | ||
|
||
type availableIndices struct { | ||
indices []*model.IndexInfo | ||
includeTableScan bool | ||
func (ds *DataSource) deriveIndexPathStats(path *accessPath) error { | ||
var err error | ||
sc := ds.ctx.GetSessionVars().StmtCtx | ||
path.ranges = ranger.FullRange() | ||
path.countAfterAccess = float64(ds.statisticTable.Count) | ||
idxCols, lengths := expression.IndexInfo2Cols(ds.schema.Columns, path.index) | ||
if len(idxCols) != 0 { | ||
path.ranges, path.accessConds, path.indexFilters, path.eqCondCount, err = ranger.DetachCondAndBuildRangeForIndex(ds.ctx, ds.pushedDownConds, idxCols, lengths) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
path.countAfterAccess, err = ds.statisticTable.GetRowCountByIndexRanges(sc, path.index.ID, path.ranges) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
path.indexFilters, path.tableFilters = splitIndexFilterConditions(path.indexFilters, path.index.Columns, ds.tableInfo) | ||
} else { | ||
path.indexFilters, path.tableFilters = splitIndexFilterConditions(ds.pushedDownConds, path.index.Columns, ds.tableInfo) | ||
} | ||
path.countAfterIndex = path.countAfterAccess | ||
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. Duplicated with line 364? 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. removed one. |
||
if path.indexFilters != nil { | ||
selectivity, err := ds.statisticTable.Selectivity(ds.ctx, path.indexFilters) | ||
if err != nil { | ||
log.Warnf("An error happened: %v, we have to use the default selectivity", err.Error()) | ||
selectivity = selectionFactor | ||
} | ||
path.countAfterIndex = math.Max(path.countAfterAccess*selectivity, ds.statsAfterSelect.count) | ||
} | ||
return nil | ||
} | ||
|
||
func (ds *DataSource) getPKIsHandleCol() *expression.Column { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We'better add some test cases to cover pkCol == nil and pkCol !=nil.
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.
the status of pkCol is covered executor's unit test
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.
But why all test cases can be passed when line 336~340 was missed before,
we may need some test for checking this case.
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.
let me check it first.