diff --git a/executor/adapter.go b/executor/adapter.go index 5d076c2f4b456..e106a81343355 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -1195,7 +1195,9 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) { binaryPlan := "" if variable.GenerateBinaryPlan.Load() { binaryPlan = getBinaryPlan(a.Ctx) - binaryPlan = variable.SlowLogBinaryPlanPrefix + binaryPlan + variable.SlowLogPlanSuffix + if len(binaryPlan) > 0 { + binaryPlan = variable.SlowLogBinaryPlanPrefix + binaryPlan + variable.SlowLogPlanSuffix + } } resultRows := GetResultRowsCount(stmtCtx, a.Plan) diff --git a/planner/core/binary_plan_test.go b/planner/core/binary_plan_test.go index be8aadd6e7f4c..40abb5c62b493 100644 --- a/planner/core/binary_plan_test.go +++ b/planner/core/binary_plan_test.go @@ -17,6 +17,7 @@ package core_test import ( "encoding/base64" "fmt" + "io/ioutil" "os" "regexp" "strings" @@ -502,3 +503,32 @@ func TestInvalidDecodeBinaryPlan(t *testing.T) { tk.MustQuery(`select tidb_decode_binary_plan('` + str3 + `')`).Check(testkit.Rows("")) tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 proto: illegal wireType 7")) } + +func TestUnnecessaryBinaryPlanInSlowLog(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + tk := testkit.NewTestKit(t, store) + require.True(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) + + originCfg := config.GetGlobalConfig() + newCfg := *originCfg + f, err := os.CreateTemp("", "tidb-slow-*.log") + require.NoError(t, err) + newCfg.Log.SlowQueryFile = f.Name() + config.StoreGlobalConfig(&newCfg) + defer func() { + config.StoreGlobalConfig(originCfg) + require.NoError(t, f.Close()) + require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) + }() + require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) + + tk.MustExec("use test") + tk.MustExec("drop table if exists th") + tk.MustExec("set global tidb_slow_log_threshold = 1;") + tk.MustExec("create table th (i int, a int,b int, c int, index (a)) partition by hash (a) partitions 100;") + slowLogBytes, err := ioutil.ReadAll(f) + require.NoError(t, err) + require.NotContains(t, string(slowLogBytes), `tidb_decode_binary_plan('')`) +}