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, config: Enable plan cache by default #20416

Merged
merged 6 commits into from Oct 29, 2020
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
8 changes: 8 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/metrics"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/cluster"
Expand Down Expand Up @@ -604,6 +605,13 @@ func (s *testSuite) TestErrorBind(c *C) {
func (s *testSuite) TestPreparedStmt(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)

orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
}()
plannercore.SetPreparedPlanCache(false) // requires plan cache disabled, or the IndexNames = 1 on first test.

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx(a))")
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ var defaultConf = Config{
HeaderTimeout: 5,
},
PreparedPlanCache: PreparedPlanCache{
Enabled: false,
Enabled: true,
Capacity: 100,
MemoryGuardRatio: 0.1,
},
Expand Down
2 changes: 1 addition & 1 deletion config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ networks = ""
header-timeout = 5

[prepared-plan-cache]
enabled = false
enabled = true
capacity = 100
memory-guard-ratio = 0.1

Expand Down
6 changes: 6 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func (s *testSerialSuite) TestPrepareStmtAfterIsolationReadChange(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost", CurrentUser: true, AuthUsername: "root", AuthHostname: "%"}, nil, []byte("012345678901234567890"))

orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
}()
plannercore.SetPreparedPlanCache(false) // requires plan cache disabled

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
// create virtual tiflash replica.
Expand Down
18 changes: 18 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5593,6 +5593,24 @@ func (s *testIntegrationSuite) TestCastStrToInt(c *C) {
}
}

func (s *testIntegrationSerialSuite) TestPreparePlanCache(c *C) {
tk := testkit.NewTestKit(c, s.store)

// Plan cache should now be on by default
c.Assert(plannercore.PreparedPlanCacheEnabled(), Equals, true)

// Use the example from the docs https://docs.pingcap.com/tidb/stable/sql-prepare-plan-cache
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int);")
tk.MustExec("prepare stmt from 'select * from t where a = ?';")
tk.MustExec("set @a = 1;")
tk.MustExec("execute stmt using @a;")
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
tk.MustExec("execute stmt using @a;")
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
}

func (s *testIntegrationSerialSuite) TestIssue16205(c *C) {
tk := testkit.NewTestKit(c, s.store)
orgEnable := plannercore.PreparedPlanCacheEnabled()
Expand Down
5 changes: 2 additions & 3 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ import (

var (
// preparedPlanCacheEnabledValue stores the global config "prepared-plan-cache-enabled".
// If the value of "prepared-plan-cache-enabled" is true, preparedPlanCacheEnabledValue's value is 1.
// Otherwise, preparedPlanCacheEnabledValue's value is 0.
preparedPlanCacheEnabledValue int32
// The value is true unless "prepared-plan-cache-enabled" is FALSE in configuration.
preparedPlanCacheEnabledValue int32 = 1
// PreparedPlanCacheCapacity stores the global config "prepared-plan-cache-capacity".
PreparedPlanCacheCapacity uint = 100
// PreparedPlanCacheMemoryGuardRatio stores the global config "prepared-plan-cache-memory-guard-ratio".
Expand Down