-
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
planner: refactor some code related to IndexMerge and MVIndex #40800
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1418ae6
fixup
qw4990 4a0ce06
Merge branch 'master' into refactor-mvindexmerge
qw4990 32900a4
Merge branch 'master' into refactor-mvindexmerge
qw4990 0ca2f58
fixup
qw4990 d6a3ba9
Merge branch 'master' into refactor-mvindexmerge
qw4990 e3b2b8a
Merge branch 'master' into refactor-mvindexmerge
hawkingrei c6b4240
Merge branch 'master' into refactor-mvindexmerge
qw4990 e715438
Merge branch 'master' into refactor-mvindexmerge
qw4990 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
package core | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strings" | ||
|
||
|
@@ -37,6 +36,16 @@ import ( | |
|
||
// generateIndexMergePath generates IndexMerge AccessPaths on this DataSource. | ||
func (ds *DataSource) generateIndexMergePath() error { | ||
var warningMsg string | ||
stmtCtx := ds.ctx.GetSessionVars().StmtCtx | ||
defer func() { | ||
if len(ds.indexMergeHints) > 0 && warningMsg != "" { | ||
ds.indexMergeHints = nil | ||
stmtCtx.AppendWarning(errors.Errorf(warningMsg)) | ||
logutil.BgLogger().Debug(warningMsg) | ||
} | ||
}() | ||
|
||
// Consider the IndexMergePath. Now, we just generate `IndexMergePath` in DNF case. | ||
// Use allConds instread of pushedDownConds, | ||
// because we want to use IndexMerge even if some expr cannot be pushed to TiKV. | ||
|
@@ -46,11 +55,26 @@ func (ds *DataSource) generateIndexMergePath() error { | |
indexMergeConds = append(indexMergeConds, expression.PushDownNot(ds.ctx, expr)) | ||
} | ||
|
||
stmtCtx := ds.ctx.GetSessionVars().StmtCtx | ||
isPossibleIdxMerge := len(indexMergeConds) > 0 && // have corresponding access conditions, and | ||
(len(ds.possibleAccessPaths) > 1 || // (have multiple index paths, or | ||
(len(ds.possibleAccessPaths) == 1 && isMVIndexPath(ds.possibleAccessPaths[0]))) // have a MVIndex) | ||
|
||
if !isPossibleIdxMerge { | ||
warningMsg = "IndexMerge is inapplicable or disabled. No available filter or available index." | ||
return nil | ||
} | ||
|
||
sessionAndStmtPermission := (ds.ctx.GetSessionVars().GetEnableIndexMerge() || len(ds.indexMergeHints) > 0) && !stmtCtx.NoIndexMergeHint | ||
if !sessionAndStmtPermission { | ||
warningMsg = "IndexMerge is inapplicable or disabled. Got no_index_merge hint or tidb_enable_index_merge is off." | ||
return nil | ||
} | ||
|
||
if ds.tableInfo.TempTableType == model.TempTableLocal { | ||
warningMsg = "IndexMerge is inapplicable or disabled. Cannot use IndexMerge on temporary table." | ||
return nil | ||
} | ||
|
||
// We current do not consider `IndexMergePath`: | ||
// 1. If there is an index path. | ||
// 2. TODO: If there exists exprs that cannot be pushed down. This is to avoid wrongly estRow of Selection added by rule_predicate_push_down. | ||
|
@@ -87,26 +111,37 @@ func (ds *DataSource) generateIndexMergePath() error { | |
} | ||
} | ||
|
||
if isPossibleIdxMerge && sessionAndStmtPermission && needConsiderIndexMerge && ds.tableInfo.TempTableType != model.TempTableLocal { | ||
err := ds.generateAndPruneIndexMergePath(indexMergeConds, ds.indexMergeHints != nil) | ||
if err != nil { | ||
return err | ||
} | ||
} else if len(ds.indexMergeHints) > 0 { | ||
if !needConsiderIndexMerge { | ||
warningMsg = "IndexMerge is inapplicable or disabled. " | ||
return nil | ||
} | ||
regularPathCount := len(ds.possibleAccessPaths) | ||
if err := ds.generateAndPruneIndexMergePath(indexMergeConds, ds.indexMergeHints != nil); err != nil { | ||
return err | ||
} | ||
|
||
// If without hints, it means that `enableIndexMerge` is true | ||
if len(ds.indexMergeHints) == 0 { | ||
return nil | ||
} | ||
// If len(indexMergeHints) > 0, then add warnings if index-merge hints cannot work. | ||
if regularPathCount == len(ds.possibleAccessPaths) { | ||
ds.indexMergeHints = nil | ||
var msg string | ||
if !isPossibleIdxMerge { | ||
msg = "No available filter or available index." | ||
} else if !sessionAndStmtPermission { | ||
msg = "Got no_index_merge hint or tidb_enable_index_merge is off." | ||
} else if ds.tableInfo.TempTableType == model.TempTableLocal { | ||
msg = "Cannot use IndexMerge on temporary table." | ||
} | ||
msg = fmt.Sprintf("IndexMerge is inapplicable or disabled. %s", msg) | ||
stmtCtx.AppendWarning(errors.Errorf(msg)) | ||
logutil.BgLogger().Debug(msg) | ||
ds.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("IndexMerge is inapplicable")) | ||
return nil | ||
} | ||
|
||
// If len(indexMergeHints) > 0 and some index-merge paths were added, then prune all other non-index-merge paths. | ||
ds.possibleAccessPaths = ds.possibleAccessPaths[regularPathCount:] | ||
minRowCount := ds.possibleAccessPaths[0].CountAfterAccess | ||
for _, path := range ds.possibleAccessPaths { | ||
if minRowCount < path.CountAfterAccess { | ||
minRowCount = path.CountAfterAccess | ||
} | ||
} | ||
if ds.stats.RowCount > minRowCount { | ||
ds.stats = ds.tableStats.ScaleByExpectCnt(minRowCount) | ||
} | ||
return nil | ||
} | ||
|
||
|
@@ -461,36 +496,6 @@ func (ds *DataSource) generateAndPruneIndexMergePath(indexMergeConds []expressio | |
if mvIndexMergePath != nil { | ||
ds.possibleAccessPaths = append(ds.possibleAccessPaths, mvIndexMergePath...) | ||
} | ||
|
||
// 4. If needed, append a warning if no IndexMerge is generated. | ||
|
||
// If without hints, it means that `enableIndexMerge` is true | ||
if len(ds.indexMergeHints) == 0 { | ||
return nil | ||
} | ||
// With hints and without generated IndexMerge paths | ||
if regularPathCount == len(ds.possibleAccessPaths) { | ||
ds.indexMergeHints = nil | ||
ds.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("IndexMerge is inapplicable")) | ||
return nil | ||
} | ||
|
||
// 4. If needPrune is true, prune non-IndexMerge paths. | ||
|
||
// Do not need to consider the regular paths in find_best_task(). | ||
// So we can use index merge's row count as DataSource's row count. | ||
if needPrune { | ||
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. Maybe we can remove the argument 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. You are right. It was removed now. |
||
ds.possibleAccessPaths = ds.possibleAccessPaths[regularPathCount:] | ||
minRowCount := ds.possibleAccessPaths[0].CountAfterAccess | ||
for _, path := range ds.possibleAccessPaths { | ||
if minRowCount < path.CountAfterAccess { | ||
minRowCount = path.CountAfterAccess | ||
} | ||
} | ||
if ds.stats.RowCount > minRowCount { | ||
ds.stats = ds.tableStats.ScaleByExpectCnt(minRowCount) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
|
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.
It seems that
minRowCount
is the maximum of each path'sCountAfterAccess
, which is confusing. Since the PR is a refactor, we can solve the problem in the future PR.