-
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
plan: calc access path
when doing deriveStats
.
#6346
Changes from 3 commits
391e389
2039b18
50fd959
ba77bdf
d1a7691
8192bfa
5d3620f
d170ac3
e528c54
8d5775b
e6bc81b
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 |
---|---|---|
|
@@ -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,88 @@ type DataSource struct { | |
|
||
statisticTable *statistics.Table | ||
|
||
// availableIndices is used for storing result of availableIndices function. | ||
availableIndices *availableIndices | ||
// possibleAccessPaths stores all the possible index path for physical plan, including table scan. | ||
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. all the possible access paths |
||
// Please make sure table scan is always the first element. | ||
possibleAccessPaths []*accessPath | ||
} | ||
|
||
// accessPath tells how we access one index or just access table. | ||
type accessPath struct { | ||
index *model.IndexInfo | ||
ranges []*ranger.NewRange | ||
// 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 | ||
// isRowID indicates this path stores the information for table scan. | ||
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. s/ this path stores the information for table scan/ whether this path is a table path |
||
isRowID bool | ||
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. why not just name it as isTablePath? |
||
// forced means this index is generated by `use/force index()`. | ||
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. s/ this index/ this path |
||
forced bool | ||
} | ||
|
||
type availableIndices struct { | ||
indices []*model.IndexInfo | ||
includeTableScan bool | ||
func (ds *DataSource) prepareTablePath(path *accessPath) error { | ||
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. Since it's called by 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. Will this be more concise? func (ds *DataSource) prepareTablePath(path *accessPath) error {
var err error
var pkCol *expression.Column
sc := ds.ctx.GetSessionVars().StmtCtx
path.countAfterAccess = float64(ds.statisticTable.Count)
path.ranges = ranger.FullIntNewRange(false)
path.tableFilters = ds.pushedDownConds
if pkCol == nil {
return nil
}
path.ranges = ranger.FullIntNewRange(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)
} |
||
var err error | ||
sc := ds.ctx.GetSessionVars().StmtCtx | ||
path.countAfterAccess = float64(ds.statisticTable.Count) | ||
var pkCol *expression.Column | ||
if pkCol != nil { | ||
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. when will |
||
path.ranges = ranger.FullIntNewRange(mysql.HasUnsignedFlag(pkCol.RetType.Flag)) | ||
} else { | ||
path.ranges = ranger.FullIntNewRange(false) | ||
} | ||
path.countAfterAccess = float64(ds.statisticTable.Count) | ||
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 333? |
||
if len(ds.pushedDownConds) > 0 { | ||
if pkCol != nil { | ||
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. ditto |
||
path.accessConds, path.tableFilters = ranger.DetachCondsForTableRange(ds.ctx, ds.pushedDownConds, pkCol) | ||
path.ranges, err = ranger.BuildTableRange(path.accessConds, sc, pkCol.RetType) | ||
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. We may put sc as the first parameter. |
||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
path.countAfterAccess, err = ds.statisticTable.GetRowCountByIntColumnRanges(sc, pkCol.ID, path.ranges) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} else { | ||
path.tableFilters = ds.pushedDownConds | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ds *DataSource) prepareIndexPath(path *accessPath) error { | ||
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. ditto |
||
var err error | ||
sc := ds.ctx.GetSessionVars().StmtCtx | ||
path.ranges = ranger.FullNewRange() | ||
path.countAfterAccess = float64(ds.statisticTable.Count) | ||
path.countAfterIndex = 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 { | ||
|
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.
why not use
len(indexPaths) > 0 ...