(t *testing.T)
| 31 | ) |
| 32 | |
| 33 | func TestPointGetPlanCache(t *testing.T) { |
| 34 | store := testkit.CreateMockStore(t) |
| 35 | tk := testkit.NewTestKit(t, store) |
| 36 | tk.MustExec(`set @@tidb_enable_non_prepared_plan_cache=0`) // affect hit counter in this ut |
| 37 | tk.MustExec(`set tidb_enable_prepared_plan_cache=1`) |
| 38 | tk.MustExec("use test") |
| 39 | tk.MustExec("drop table if exists t") |
| 40 | tk.MustExec("create table t(a bigint unsigned primary key, b int, c int, key idx_bc(b,c))") |
| 41 | tk.MustExec("insert into t values(1, 1, 1), (2, 2, 2), (3, 3, 3)") |
| 42 | tk.MustQuery("explain format = 'brief' select * from t where a = 1").Check(testkit.Rows( |
| 43 | "Point_Get 1.00 root table:t handle:1", |
| 44 | )) |
| 45 | tk.MustQuery("explain format = 'brief' select * from t where 1 = a").Check(testkit.Rows( |
| 46 | "Point_Get 1.00 root table:t handle:1", |
| 47 | )) |
| 48 | tk.MustQuery("explain format = 'brief' update t set b=b+1, c=c+1 where a = 1").Check(testkit.Rows( |
| 49 | "Update N/A root N/A", |
| 50 | "└─Point_Get 1.00 root table:t handle:1", |
| 51 | )) |
| 52 | tk.MustQuery("explain format = 'brief' delete from t where a = 1").Check(testkit.Rows( |
| 53 | "Delete N/A root N/A", |
| 54 | "└─Point_Get 1.00 root table:t handle:1", |
| 55 | )) |
| 56 | tk.MustQuery("explain format = 'brief' select a from t where a = -1").Check(testkit.Rows( |
| 57 | "TableDual 0.00 root rows:0", |
| 58 | )) |
| 59 | tk.MustExec(`prepare stmt0 from "select a from t where a = ?"`) |
| 60 | tk.MustExec("set @p0 = -1") |
| 61 | tk.MustQuery("execute stmt0 using @p0").Check(testkit.Rows()) |
| 62 | metrics.ResettablePlanCacheCounterFortTest = true |
| 63 | metrics.PlanCacheCounter.Reset() |
| 64 | counter := metrics.PlanCacheCounter.WithLabelValues("prepare") |
| 65 | pb := &dto.Metric{} |
| 66 | var hit float64 |
| 67 | // PointGetPlan for Select. |
| 68 | tk.MustExec(`prepare stmt1 from "select * from t where a = ?"`) |
| 69 | tk.MustExec(`prepare stmt2 from "select * from t where b = ? and c = ?"`) |
| 70 | tk.MustExec("set @param=1") |
| 71 | tk.MustQuery("execute stmt1 using @param").Check(testkit.Rows("1 1 1")) |
| 72 | err := counter.Write(pb) |
| 73 | require.NoError(t, err) |
| 74 | hit = pb.GetCounter().GetValue() |
| 75 | require.Equal(t, float64(0), hit) |
| 76 | tk.MustExec("set @param=2") |
| 77 | tk.MustQuery("execute stmt1 using @param").Check(testkit.Rows("2 2 2")) |
| 78 | err = counter.Write(pb) |
| 79 | require.NoError(t, err) |
| 80 | hit = pb.GetCounter().GetValue() |
| 81 | require.Equal(t, float64(1), hit) |
| 82 | tk.MustQuery("execute stmt2 using @param, @param").Check(testkit.Rows("2 2 2")) |
| 83 | err = counter.Write(pb) |
| 84 | require.NoError(t, err) |
| 85 | hit = pb.GetCounter().GetValue() |
| 86 | require.Equal(t, float64(1), hit) |
| 87 | tk.MustExec("set @param=1") |
| 88 | tk.MustQuery("execute stmt2 using @param, @param").Check(testkit.Rows("1 1 1")) |
| 89 | err = counter.Write(pb) |
| 90 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected