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

sql/analyzer/indexed_joins.go: Use partial indexes for indexed joins when a full index isn't available. #325

Merged
merged 2 commits into from Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions enginetest/query_plans.go
Expand Up @@ -262,6 +262,15 @@ var PlanTests = []QueryPlanTest{
" └─ IndexedTableAccess(othertable on [othertable.i2])\n" +
"",
},
{
Query: "SELECT /*+ JOIN_ORDER(mt, o) */ * FROM mytable mt INNER JOIN one_pk o ON mt.i = o.pk AND mt.s = o.c2",
ExpectedPlan: "IndexedJoin(mt.i = o.pk AND mt.s = o.c2)\n" +
" ├─ TableAlias(mt)\n" +
" │ └─ Table(mytable)\n" +
" └─ TableAlias(o)\n" +
" └─ IndexedTableAccess(one_pk on [one_pk.pk])\n" +
"",
},
{
Query: "SELECT i, i2, s2 FROM mytable RIGHT JOIN othertable ON i = i2 - 1",
ExpectedPlan: "Project(mytable.i, othertable.i2, othertable.s2)\n" +
Expand Down
14 changes: 13 additions & 1 deletion sql/analyzer/indexed_joins.go
Expand Up @@ -681,7 +681,19 @@ func getJoinIndex(
exprsByTable := joinExprsByTable(joinCondPredicates)
indexesByTable := make(joinIndexesByTable)
for table, cols := range exprsByTable {
idx := ia.IndexByExpression(ctx, ctx.GetCurrentDatabase(), normalizeExpressions(tableAliases, extractExpressions(cols)...)...)
exprs := extractExpressions(cols)
idx := ia.IndexByExpression(ctx, ctx.GetCurrentDatabase(), normalizeExpressions(tableAliases, exprs...)...)
// If we do not find a perfect index, we take the first single column partial index if there is one.
// This currently only finds single column indexes. A better search would look for the most complete
// index available, covering the columns with the most specificity / highest cardinality.
if idx == nil && len(exprs) > 1 {
for _, e := range exprs {
idx = ia.IndexByExpression(ctx, ctx.GetCurrentDatabase(), normalizeExpressions(tableAliases, e)...)
if idx != nil {
break
}
}
}
indexesByTable[table] = append(indexesByTable[table], colExprsToJoinIndex(table, idx, joinCond, cols))
}

Expand Down