Skip to content

Commit

Permalink
planner: allow the optimizer to cache query plans accessing generated…
Browse files Browse the repository at this point in the history
… columns by default (#51654)

close #45798
  • Loading branch information
qw4990 authored Mar 11, 2024
1 parent 715399f commit f2cbe00
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
3 changes: 1 addition & 2 deletions pkg/executor/explainfor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,7 @@ func TestIndexMerge4PlanCache(t *testing.T) {
tk.MustExec("prepare stmt from 'SELECT /*+ USE_INDEX_MERGE(t0, i0, PRIMARY)*/ t0.c0 FROM t0 WHERE t0.c1 OR t0.c0;';")
tk.MustQuery("execute stmt;").Check(testkit.Rows("1"))
tk.MustQuery("execute stmt;").Check(testkit.Rows("1"))
// The plan contains the generated column, so it can not be cached.
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))

tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(id int primary key, a int, b int, c int, d int)")
Expand Down
6 changes: 2 additions & 4 deletions pkg/planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,8 @@ func TestNonPreparedPlanExplainWarning(t *testing.T) {
"select distinct a from t1 where a > 1 and b < 2", // distinct
"select count(*) from t1 where a > 1 and b < 2 group by a", // group by
"select * from t1 order by a", // order by
"select * from t3 where full_name = 'a b'", // generated column
"select * from t3 where a > 1 and full_name = 'a b'",
}

unsupported := []string{
Expand All @@ -1022,8 +1024,6 @@ func TestNonPreparedPlanExplainWarning(t *testing.T) {
"select * from t where bt > 0", // bit
"select * from t where a > 1 and bt > 0",
"select data_type from INFORMATION_SCHEMA.columns where table_name = 'v'", // memTable
"select * from t3 where full_name = 'a b'", // generated column
"select * from t3 where a > 1 and full_name = 'a b'",
"select * from v", // view
"select * from t where a = null", // null
"select * from t where false", // table dual
Expand All @@ -1044,8 +1044,6 @@ func TestNonPreparedPlanExplainWarning(t *testing.T) {
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: access tables in system schema",
"skip non-prepared plan-cache: query accesses generated columns is un-cacheable",
"skip non-prepared plan-cache: query accesses generated columns is un-cacheable",
"skip non-prepared plan-cache: queries that access views are not supported",
"skip non-prepared plan-cache: query has null constants",
"skip non-prepared plan-cache: some parameters may be overwritten when constant propagation",
Expand Down
6 changes: 3 additions & 3 deletions pkg/planner/core/plan_cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,11 @@ func getMaxParamLimit(sctx PlanContext) int {

func enablePlanCacheForGeneratedCols(sctx PlanContext) bool {
// disable this by default since it's not well tested.
// TODO: complete its test and enable it by default.
defaultVal := true
if sctx == nil || sctx.GetSessionVars() == nil || sctx.GetSessionVars().GetOptimizerFixControlMap() == nil {
return false
return defaultVal
}
return fixcontrol.GetBoolWithDefault(sctx.GetSessionVars().GetOptimizerFixControlMap(), fixcontrol.Fix45798, false)
return fixcontrol.GetBoolWithDefault(sctx.GetSessionVars().GetOptimizerFixControlMap(), fixcontrol.Fix45798, defaultVal)
}

// checkTableCacheable checks whether a query accessing this table is cacheable.
Expand Down
4 changes: 1 addition & 3 deletions tests/integrationtest/r/planner/core/indexmerge_path.result
Original file line number Diff line number Diff line change
Expand Up @@ -470,15 +470,13 @@ Selection 23.98 root json_overlaps(json_extract(planner__core__indexmerge_path.
drop table if exists t;
create table t(j json, index kj((cast(j as signed array))));
prepare st from 'select /*+ use_index_merge(t, kj) */ * from t where (1 member of (j))';
Level Code Message
Warning 1105 skip prepared plan-cache: query accesses generated columns is un-cacheable
execute st;
j
execute st;
j
select @@last_plan_from_cache;
@@last_plan_from_cache
0
1
drop table if exists t;
create table t(j json, unique kj((cast(j as signed array))));
explain select j from t where j=1;
Expand Down

0 comments on commit f2cbe00

Please sign in to comment.