Skip to content
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

planner: enhance index join for more scenarios #10540

Merged
merged 6 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"math"
"strings"

"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
Expand Down Expand Up @@ -481,25 +482,29 @@ type indexJoinBuildHelper struct {
}

func (ijHelper *indexJoinBuildHelper) buildRangeDecidedByInformation(idxCols []*expression.Column, outerJoinKeys []*expression.Column) string {
buffer := bytes.NewBufferString("[")
isFirst := true
var builder strings.Builder
builder.WriteString("[")
accessConds := make([]string, 0, len(ijHelper.curPossibleUsedKeys)+len(ijHelper.chosenAccess))
for idxOff, keyOff := range ijHelper.idxOff2KeyOff {
if keyOff == -1 {
continue
}
accessConds = append(accessConds, fmt.Sprintf("eq(%v, %v)", idxCols[idxOff], outerJoinKeys[keyOff]))
}
for _, access := range ijHelper.chosenAccess {
accessConds = append(accessConds, fmt.Sprintf("%v", access))
}
isFirst := true
for _, cond := range accessConds {
if !isFirst {
buffer.WriteString(" ")
builder.WriteString(" ")
} else {
isFirst = false
}
buffer.WriteString(fmt.Sprintf("eq(%v, %v)", idxCols[idxOff], outerJoinKeys[keyOff]))
}
for _, access := range ijHelper.chosenAccess {
// Since now there must be eq/in condition so here we can just append space directly.
buffer.WriteString(fmt.Sprintf(" %v", access))
builder.WriteString(cond)
}
buffer.WriteString("]")
return buffer.String()
builder.WriteString("]")
return builder.String()
}

// constructInnerTableScan is specially used to construct the inner plan for PhysicalIndexJoin.
Expand Down Expand Up @@ -678,22 +683,19 @@ func (cwc *ColWithCmpFuncManager) String() string {
return buffer.String()
}

func (ijHelper *indexJoinBuildHelper) checkIndex(innerKeys []*expression.Column, idxCols []*expression.Column, colLens []int) bool {
func (ijHelper *indexJoinBuildHelper) resetContextForIndex(innerKeys []*expression.Column, idxCols []*expression.Column, colLens []int) {
tmpSchema := expression.NewSchema(innerKeys...)
ijHelper.curIdxOff2KeyOff = make([]int, len(idxCols))
ijHelper.curNotUsedIndexCols = make([]*expression.Column, 0, len(idxCols))
ijHelper.curNotUsedColLens = make([]int, 0, len(idxCols))
keyMatched := false
for i, idxCol := range idxCols {
ijHelper.curIdxOff2KeyOff[i] = tmpSchema.ColumnIndex(idxCol)
if ijHelper.curIdxOff2KeyOff[i] >= 0 {
keyMatched = true
continue
}
ijHelper.curNotUsedIndexCols = append(ijHelper.curNotUsedIndexCols, idxCol)
ijHelper.curNotUsedColLens = append(ijHelper.curNotUsedColLens, colLens[i])
}
return keyMatched
}

// findUsefulEqAndInFilters analyzes the pushedDownConds held by inner child and split them to three parts.
Expand Down Expand Up @@ -791,26 +793,28 @@ func (ijHelper *indexJoinBuildHelper) analyzeLookUpFilters(indexInfo *model.Inde
return nil
}
accesses := make([]expression.Expression, 0, len(idxCols))
// If no index column appears in join key, we just break.
// TODO: It may meet this case: There's no join key condition, but have compare filters.
// e.g. select * from t1, t2 on t1.a=t2.a and t2.b > t1.b-10 and t2.b < t1.b where t1.a=1 and t2.a=1.
// After constant propagation. The t1.a=t2.a is removed. And if we have index (t2.a, t2.b). It can apply index join
// to speed up.
if !ijHelper.checkIndex(innerJoinKeys, idxCols, colLengths) {
return nil
}
ijHelper.resetContextForIndex(innerJoinKeys, idxCols, colLengths)
notKeyEqAndIn, remained, rangeFilterCandidates := ijHelper.findUsefulEqAndInFilters(innerPlan)
var remainedEqAndIn []expression.Expression
notKeyEqAndIn, remainedEqAndIn = ijHelper.removeUselessEqAndInFunc(idxCols, notKeyEqAndIn)
matchedKeyCnt := len(ijHelper.curPossibleUsedKeys)
if matchedKeyCnt <= 0 {
// If no join key is matched while join keys actually are not empty. We don't choose index join for now.
if matchedKeyCnt <= 0 && len(innerJoinKeys) > 0 {
return nil
}
accesses = append(accesses, notKeyEqAndIn...)
remained = append(remained, remainedEqAndIn...)
lastColPos := matchedKeyCnt + len(notKeyEqAndIn)
// There should be some equal conditions. But we don't need that there must be some join key in accesses here.
// A more strict check is applied later.
if lastColPos <= 0 {
return nil
}
// If all the index columns are covered by eq/in conditions, we don't need to consider other conditions anymore.
if lastColPos == len(idxCols) {
if matchedKeyCnt <= 0 {
return nil
}
remained = append(remained, rangeFilterCandidates...)
ranges, err := ijHelper.buildTemplateRange(matchedKeyCnt, notKeyEqAndIn, nil, false)
if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ func (s *testPlanSuite) TestDAGPlanBuilderJoin(c *C) {
},
// Test Index Join + DoubleRead.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ * from t t1, t t2 where t1.a = t2.c",
sql: "select /*+ TIDB_INLJ(t2) */ * from t t1, t t2 where t1.a = t2.c",
best: "IndexJoin{TableReader(Table(t))->IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))}(test.t1.a,test.t2.c)",
},
// Test Index Join + SingleRead.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ t1.a , t2.a from t t1, t t2 where t1.a = t2.c",
sql: "select /*+ TIDB_INLJ(t2) */ t1.a , t2.a from t t1, t t2 where t1.a = t2.c",
best: "IndexJoin{TableReader(Table(t))->IndexReader(Index(t.c_d_e)[[NULL,+inf]])}(test.t1.a,test.t2.c)->Projection",
},
// Test Index Join + Order by.
Expand Down Expand Up @@ -424,6 +424,22 @@ func (s *testPlanSuite) TestDAGPlanBuilderJoin(c *C) {
sql: "select /*+ TIDB_INLJ(t2) */ * from t t1 join t t2 where t1.a=t2.a and t2.a in (1, 2)",
best: "IndexJoin{TableReader(Table(t))->TableReader(Table(t)->Sel([in(test.t2.a, 1, 2)]))}(test.t1.a,test.t2.a)",
},
{
sql: "select /*+ TIDB_INLJ(t2) */ * from t t1 join t t2 where t1.b=t2.c and t1.b=1 and t2.d > t1.d-10 and t2.d < t1.d+10",
best: "IndexJoin{TableReader(Table(t)->Sel([eq(test.t1.b, 1)]))->IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))}",
},
{
sql: "select /*+ TIDB_INLJ(t2) */ * from t t1 join t t2 where t1.b=t2.b and t1.c=1 and t2.c=1 and t2.d > t1.d-10 and t2.d < t1.d+10",
best: "LeftHashJoin{IndexLookUp(Index(t.c_d_e)[[1,1]], Table(t))->IndexLookUp(Index(t.c_d_e)[[1,1]], Table(t))}(test.t1.b,test.t2.b)",
},
{
sql: "select /*+ TIDB_INLJ(t2) */ * from t t1 join t t2 where t2.c > t1.d-10 and t2.c < t1.d+10",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}",
},
{
sql: "select /*+ TIDB_INLJ(t2) */ * from t t1 join t t2 where t1.b = t2.c and t2.c=1 and t2.d=2 and t2.e=4",
best: "LeftHashJoin{TableReader(Table(t)->Sel([eq(test.t1.b, 1)]))->IndexLookUp(Index(t.c_d_e)[[1 2 4,1 2 4]], Table(t))}",
},
}
for i, tt := range tests {
comment := Commentf("case:%v sql:%s", i, tt.sql)
Expand Down