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: check the ignore-plan-cache hint in insert-stmt (#40080) #40693

Closed
wants to merge 14 commits into from
10 changes: 5 additions & 5 deletions executor/seqtest/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,14 @@ func TestPreparedInsert(t *testing.T) {
err = counter.Write(pb)
require.NoError(t, err)
hit := pb.GetCounter().GetValue()
require.Equal(t, float64(1), hit)
require.Equal(t, float64(0), hit)
}
tk.MustExec(`set @a=3,@b=3; execute stmt_insert using @a, @b;`)
if flag {
err = counter.Write(pb)
require.NoError(t, err)
hit := pb.GetCounter().GetValue()
require.Equal(t, float64(2), hit)
require.Equal(t, float64(0), hit)
}

result := tk.MustQuery("select id, c1 from prepare_test where id = ?", 1)
Expand All @@ -518,21 +518,21 @@ func TestPreparedInsert(t *testing.T) {
err = counter.Write(pb)
require.NoError(t, err)
hit := pb.GetCounter().GetValue()
require.Equal(t, float64(2), hit)
require.Equal(t, float64(0), hit)
}
tk.MustExec(`set @a=2; execute stmt_insert_select using @a;`)
if flag {
err = counter.Write(pb)
require.NoError(t, err)
hit := pb.GetCounter().GetValue()
require.Equal(t, float64(3), hit)
require.Equal(t, float64(1), hit)
}
tk.MustExec(`set @a=3; execute stmt_insert_select using @a;`)
if flag {
err = counter.Write(pb)
require.NoError(t, err)
hit := pb.GetCounter().GetValue()
require.Equal(t, float64(4), hit)
require.Equal(t, float64(2), hit)
}

result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 101)
Expand Down
13 changes: 13 additions & 0 deletions planner/core/cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ func (checker *cacheableChecker) Enter(in ast.Node) (out ast.Node, skipChildren
return in, true
}
}
case *ast.InsertStmt:
if node.Select == nil {
// do not cache insert-values-stmt like 'insert into t values (...)' since
// no performance benefit and to save memory.
checker.cacheable = false
return in, true
}
for _, hints := range node.TableHints {
if hints.HintName.L == HintIgnorePlanCache {
checker.cacheable = false
return in, true
}
}
case *ast.VariableExpr, *ast.ExistsSubqueryExpr, *ast.SubqueryExpr:
checker.cacheable = false
return in, true
Expand Down
28 changes: 27 additions & 1 deletion planner/core/cacheable_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ import (
"github.com/stretchr/testify/require"
)

func TestIgnoreInsertStmt(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int)")

// do not cache native insert-stmt
tk.MustExec("prepare st from 'insert into t values (1)'")
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))

// ignore-hint in insert-stmt can work
tk.MustExec("prepare st from 'insert into t select * from t'")
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec("prepare st from 'insert /*+ ignore_plan_cache() */ into t select * from t'")
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}

func TestCacheable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand All @@ -52,7 +76,9 @@ func TestCacheable(t *testing.T) {

tableRefsClause := &ast.TableRefsClause{TableRefs: &ast.Join{Left: &ast.TableSource{Source: tbl}}}
// test InsertStmt
stmt = &ast.InsertStmt{Table: tableRefsClause}
stmt = &ast.InsertStmt{Table: tableRefsClause} // insert-values-stmt
require.False(t, core.Cacheable(stmt, is))
stmt = &ast.InsertStmt{Table: tableRefsClause, Select: &ast.SelectStmt{}} // insert-select-stmt
require.True(t, core.Cacheable(stmt, is))

// test DeleteStmt
Expand Down