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: notify skyline pruning of index statistics availability #59110

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion pkg/planner/cardinality/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func TestNewIndexWithoutStats(t *testing.T) {
testKit := testkit.NewTestKit(t, store)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t(a int, b int, c int, index idxa(a))")
testKit.MustExec("create table t(a int, b int, c int, index idxa(a), index idxca(c,a))")
testKit.MustExec("set @@tidb_analyze_version=2")
testKit.MustExec("set @@global.tidb_enable_auto_analyze='OFF'")
testKit.MustExec("insert into t values (1, 1, 1)")
Expand All @@ -364,6 +364,11 @@ func TestNewIndexWithoutStats(t *testing.T) {
testKit.MustExec("create index idxab on t(a, b)")
// New index idxab should win due to having the most matching equal predicates - regardless of no statistics
testKit.MustQuery("explain format='brief' select * from t where a = 5 and b = 5").CheckContain("idxab(a, b)")
// New index idxab should win due to having the most predicates - regardless of no statistics
testKit.MustQuery("explain format='brief' select * from t where a > 5 and b > 5").CheckContain("idxab(a, b)")
testKit.MustQuery("explain format='brief' select * from t where a = 5 and b > 5 and c > 5").CheckContain("idxab(a, b)")
// New index idxab should NOT win because idxca has the same number of equals and has statistics
testKit.MustQuery("explain format='brief' select * from t where a = 5 and b > 5 and c = 5").CheckContain("idxca(c, a)")
}

func TestIssue57948(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,10 @@ func compareCandidates(sctx base.PlanContext, statsTbl *statistics.Table, tableI
threshold := float64(fixcontrol.GetIntWithDefault(sctx.GetSessionVars().OptimizerFixControl, fixcontrol.Fix45132, 1000))
if threshold > 0 { // set it to 0 to disable this rule
if lhs.path.CountAfterAccess/rhs.path.CountAfterAccess > threshold {
return -1, false
return -1, rhsPseudo
}
if rhs.path.CountAfterAccess/lhs.path.CountAfterAccess > threshold {
return 1, false
return 1, lhsPseudo
}
}
}
Expand All @@ -811,10 +811,10 @@ func compareCandidates(sctx base.PlanContext, statsTbl *statistics.Table, tableI
return 0, false
}
if accessResult >= 0 && scanResult >= 0 && matchResult >= 0 && globalResult >= 0 && sum > 0 {
return 1, false
return 1, lhsPseudo
terry1purcell marked this conversation as resolved.
Show resolved Hide resolved
}
if accessResult <= 0 && scanResult <= 0 && matchResult <= 0 && globalResult <= 0 && sum < 0 {
return -1, false
return -1, rhsPseudo
}
return 0, false
}
Expand Down