in point get use case, tuning on plan cache will cause performance drop
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Enhancement
```diff
diff --git i/pkg/bindinfo/session_handle_test.go w/pkg/bindinfo/session_handle_test.go
index 5d5bc3d7bb..1d61094767 100644
--- i/pkg/bindinfo/session_handle_test.go
+++ w/pkg/bindinfo/session_handle_test.go
@@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/pkg/server"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/util/stmtsummary"
+ "github.com/pingcap/tidb/pkg/util/tracing"
"github.com/stretchr/testify/require"
)
@@ -228,6 +229,36 @@ func TestBaselineDBLowerCase(t *testing.T) {
))
}
+func TestComparePointGet(t *testing.T) {
+ store := testkit.CreateMockStore(t)
+ tk := testkit.NewTestKit(t, store)
+ tk.MustExec("use test")
+ tk.MustExec("CREATE TABLE sbtest1 (" +
+ "`id` int NOT NULL AUTO_INCREMENT," +
+ "`k` int NOT NULL DEFAULT '0'," +
+ "`c` char(120) NOT NULL DEFAULT ''," +
+ "`pad` char(60) NOT NULL DEFAULT ''," +
+ "PRIMARY KEY (`id`)," +
+ "KEY `k_1` (`k`))")
+
+ ctx := context.Background()
+ duration := time.Duration(0)
+ ctx = context.WithValue(ctx, tracing.TestDurationCtxKey("test"), &duration)
+ repeat := 10_000
+ for range repeat {
+ tk.MustQueryWithContext(ctx, "select * from sbtest1 where id = 123")
+ }
+ t.Logf("average optimize time for point get: %v", duration/time.Duration(repeat))
+
+ tk.MustExec(`prepare stmt1 from 'select * from sbtest1 where id = ?'`)
+ tk.Exec("set @a = 123")
+ duration = 0
+ for range repeat {
+ tk.MustQueryWithContext(ctx, "execute stmt1 using @a")
+ }
+ t.Logf("average optimize time for point get with prepared statement: %v", duration/time.Duration(repeat))
+}
+
func TestShowGlobalBindings(t *testing.T) {
store := testkit.CreateMockStore(t)
diff --git i/pkg/planner/optimize.go w/pkg/planner/optimize.go
index 1c1f06fd6c..9534c19540 100644
--- i/pkg/planner/optimize.go
+++ w/pkg/planner/optimize.go
@@ -133,6 +133,13 @@ func getPlanFromNonPreparedPlanCache(ctx context.Context, sctx sessionctx.Contex
// Optimize does optimization and creates a Plan.
func Optimize(ctx context.Context, sctx sessionctx.Context, node *resolve.NodeW, is infoschema.InfoSchema) (plan base.Plan, slice types.NameSlice, retErr error) {
+ durP, ok := ctx.Value(tracing.TestDurationCtxKey("test")).(*time.Duration)
+ if ok {
+ now := time.Now()
+ defer func() {
+ *durP += time.Since(now)
+ }()
+ }
defer tracing.StartRegion(ctx, "planner.Optimize").End()
sessVars := sctx.GetSessionVars()
pctx := sctx.GetPlanCtx()
@@ -428,7 +435,7 @@ func allowInReadOnlyMode(sctx planctx.PlanContext, node ast.Node) (bool, error)
switch node.(type) {
// allow change variables (otherwise can't unset read-only mode)
case *ast.SetStmt,
- // allow analyze table
+ // allow analyze table
*ast.AnalyzeTableStmt,
*ast.UseStmt,
*ast.ShowStmt,
diff --git i/pkg/util/tracing/util.go w/pkg/util/tracing/util.go
index 4e5346a503..d03011c900 100644
--- i/pkg/util/tracing/util.go
+++ w/pkg/util/tracing/util.go
@@ -101,6 +101,8 @@ func StartRegion(ctx context.Context, regionType string) Region {
}
}
+type TestDurationCtxKey string
+
// StartRegionEx returns Region together with the context.
// Recommended usage is
//
```
result
```
session_handle_test.go:251: average optimize time for point get: 15.743µs
session_handle_test.go:259: average optimize time for point get with prepared statement: 52.823µs
```
Contributor guide
Assessment
This issue has not been assessed yet.