Skip to content

Commit

Permalink
plan: prepare candidate index (pingcap#5809)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored Feb 7, 2018
1 parent 1aaef04 commit fe165d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 4 additions & 1 deletion plan/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,15 @@ type DataSource struct {
// pushedDownConds are the conditions that will be pushed down to coprocessor.
pushedDownConds []expression.Expression

// relevantIndices means the indices match the push down conditions
relevantIndices []bool

// statsAfterSelect is the statsInfo for dataSource and selection.
statsAfterSelect *statsInfo

statisticTable *statistics.Table

// availableIndices is used for storing result of avalableIndices function.
// availableIndices is used for storing result of availableIndices function.
availableIndices *avalableIndices
}

Expand Down
7 changes: 5 additions & 2 deletions plan/physical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ func (ds *DataSource) convert2PhysicalPlan(prop *requiredProp) (task, error) {
return t, nil
}

// TODO: We have not checked if this table has a predicate. If not, we can only consider table scan.
indices := ds.availableIndices.indices
includeTableScan := ds.availableIndices.includeTableScan
t = invalidTask
Expand All @@ -212,7 +211,11 @@ func (ds *DataSource) convert2PhysicalPlan(prop *requiredProp) (task, error) {
}
}
if !includeTableScan || len(ds.pushedDownConds) > 0 || len(prop.cols) > 0 {
for _, idx := range indices {
for i, idx := range indices {
// TODO: We can also check if the prop matches the index columns.
if !ds.relevantIndices[i] && len(prop.cols) == 0 {
continue
}
idxTask, err := ds.convertToIndexScan(prop, idx)
if err != nil {
return nil, errors.Trace(err)
Expand Down
18 changes: 16 additions & 2 deletions plan/property_cols_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@ import (
func (ds *DataSource) preparePossibleProperties() (result [][]*expression.Column) {
indices := ds.availableIndices.indices
includeTS := ds.availableIndices.includeTableScan
ds.relevantIndices = make([]bool, len(indices))
if includeTS {
col := ds.getPKIsHandleCol()
if col != nil {
result = append(result, []*expression.Column{col})
}
cols := expression.ExtractColumnsFromExpressions(make([]*expression.Column, 0, 10), ds.pushedDownConds, nil)
for i, idx := range indices {
for _, col := range cols {
if col.ColName.L == idx.Columns[0].Name.L {
ds.relevantIndices[i] = true
break
}
}
}
} else {
for i := range ds.relevantIndices {
ds.relevantIndices[i] = true
}
}
for _, idx := range indices {
cols, _ := expression.IndexInfo2Cols(ds.schema.Columns, idx)
Expand All @@ -40,8 +54,8 @@ func (p *LogicalSelection) preparePossibleProperties() (result [][]*expression.C
}

func (p *baseLogicalPlan) preparePossibleProperties() [][]*expression.Column {
if len(p.children) > 0 {
p.children[0].preparePossibleProperties()
for _, ch := range p.children {
ch.preparePossibleProperties()
}
return nil
}
Expand Down

0 comments on commit fe165d9

Please sign in to comment.