diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index af4aa836d335a..8448a66cd0bf0 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -57,6 +57,34 @@ func loadTableStats(fileName string, dom *domain.Domain) error { return nil } +func TestExplainCostTrace(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t (a int)") + tk.MustExec("insert into t values (1)") + + tk.MustExec("set tidb_cost_model_version=2") + tk.MustQuery("explain format='cost_trace' select * from t").Check(testkit.Rows( + `TableReader_5 10000.00 177906.67 ((scan(10000*logrowsize(32)*tikv_scan_factor(40.7))) + (net(10000*rowsize(16)*tidb_kv_net_factor(3.96))))/15.00 root data:TableFullScan_4`, + `└─TableFullScan_4 10000.00 2035000.00 scan(10000*logrowsize(32)*tikv_scan_factor(40.7)) cop[tikv] table:t keep order:false, stats:pseudo`)) + tk.MustQuery("explain analyze format='cost_trace' select * from t").CheckAt([]int{0, 1, 2, 3, 4}, [][]interface{}{ + {"TableReader_5", "10000.00", "177906.67", "((scan(10000*logrowsize(32)*tikv_scan_factor(40.7))) + (net(10000*rowsize(16)*tidb_kv_net_factor(3.96))))/15.00", "1"}, + {"└─TableFullScan_4", "10000.00", "2035000.00", "scan(10000*logrowsize(32)*tikv_scan_factor(40.7))", "1"}, + }) + + tk.MustExec("set tidb_cost_model_version=1") + tk.MustQuery("explain format='cost_trace' select * from t").Check(testkit.Rows( + // cost trace on model ver1 is not supported + `TableReader_5 10000.00 34418.00 N/A root data:TableFullScan_4`, + `└─TableFullScan_4 10000.00 435000.00 N/A cop[tikv] table:t keep order:false, stats:pseudo`, + )) + tk.MustQuery("explain analyze format='cost_trace' select * from t").CheckAt([]int{0, 1, 2, 3, 4}, [][]interface{}{ + {"TableReader_5", "10000.00", "34418.00", "N/A", "1"}, + {"└─TableFullScan_4", "10000.00", "435000.00", "N/A", "1"}, + }) +} + func TestExplainAnalyze(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index 73bb25e0d874c..f11f60b95cfe5 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -742,6 +742,12 @@ func (e *Explain) prepareSchema() error { } case format == types.ExplainFormatTrueCardCost: fieldNames = []string{"id", "estRows", "estCost", "costFormula", "actRows", "task", "access object", "execution info", "operator info", "memory", "disk"} + case format == types.ExplainFormatCostTrace: + if e.Analyze || e.RuntimeStatsColl != nil { + fieldNames = []string{"id", "estRows", "estCost", "costFormula", "actRows", "task", "access object", "execution info", "operator info", "memory", "disk"} + } else { + fieldNames = []string{"id", "estRows", "estCost", "costFormula", "task", "access object", "operator info"} + } case (format == types.ExplainFormatROW || format == types.ExplainFormatBrief) && (e.Analyze || e.RuntimeStatsColl != nil): fieldNames = []string{"id", "estRows", "actRows", "task", "access object", "execution info", "operator info", "memory", "disk"} case format == types.ExplainFormatDOT: @@ -813,8 +819,18 @@ func (e *Explain) RenderResult() error { } } + if strings.ToLower(e.Format) == types.ExplainFormatCostTrace { + if pp, ok := e.TargetPlan.(PhysicalPlan); ok { + // trigger getPlanCost again with CostFlagTrace to record all cost formulas + if _, err := getPlanCost(pp, property.RootTaskType, + NewDefaultPlanCostOption().WithCostFlag(CostFlagRecalculate|CostFlagTrace)); err != nil { + return err + } + } + } + switch strings.ToLower(e.Format) { - case types.ExplainFormatROW, types.ExplainFormatBrief, types.ExplainFormatVerbose, types.ExplainFormatTrueCardCost: + case types.ExplainFormatROW, types.ExplainFormatBrief, types.ExplainFormatVerbose, types.ExplainFormatTrueCardCost, types.ExplainFormatCostTrace: if e.Rows == nil || e.Analyze { flat := FlattenPhysicalPlan(e.TargetPlan, true) e.explainFlatPlanInRowFormat(flat) @@ -995,19 +1011,23 @@ func (e *Explain) prepareOperatorInfo(p Plan, taskType, id string) { var row []string if e.Analyze || e.RuntimeStatsColl != nil { row = []string{id, estRows} - if strings.ToLower(e.Format) == types.ExplainFormatVerbose || strings.ToLower(e.Format) == types.ExplainFormatTrueCardCost { + if strings.ToLower(e.Format) == types.ExplainFormatVerbose || strings.ToLower(e.Format) == types.ExplainFormatTrueCardCost || strings.ToLower(e.Format) == types.ExplainFormatCostTrace { row = append(row, estCost) } - if strings.ToLower(e.Format) == types.ExplainFormatTrueCardCost { + if strings.ToLower(e.Format) == types.ExplainFormatTrueCardCost || strings.ToLower(e.Format) == types.ExplainFormatCostTrace { row = append(row, costFormula) } actRows, analyzeInfo, memoryInfo, diskInfo := getRuntimeInfoStr(e.ctx, p, e.RuntimeStatsColl) row = append(row, actRows, taskType, accessObject, analyzeInfo, operatorInfo, memoryInfo, diskInfo) } else { row = []string{id, estRows} - if strings.ToLower(e.Format) == types.ExplainFormatVerbose || strings.ToLower(e.Format) == types.ExplainFormatTrueCardCost { + if strings.ToLower(e.Format) == types.ExplainFormatVerbose || strings.ToLower(e.Format) == types.ExplainFormatTrueCardCost || + strings.ToLower(e.Format) == types.ExplainFormatCostTrace { row = append(row, estCost) } + if strings.ToLower(e.Format) == types.ExplainFormatCostTrace { + row = append(row, costFormula) + } row = append(row, taskType, accessObject, operatorInfo) } e.Rows = append(e.Rows, row) diff --git a/types/explain_format.go b/types/explain_format.go index 00bb9ac1e4336..9ef1dd0ccb975 100644 --- a/types/explain_format.go +++ b/types/explain_format.go @@ -35,6 +35,8 @@ var ( ExplainFormatBinary = "binary" // ExplainFormatTiDBJSON warp the default result in JSON format ExplainFormatTiDBJSON = "tidb_json" + // ExplainFormatCostTrace prints the cost and cost formula of each operator. + ExplainFormatCostTrace = "cost_trace" // ExplainFormats stores the valid formats for explain statement, used by validator. ExplainFormats = []string{ @@ -48,5 +50,6 @@ var ( ExplainFormatTrueCardCost, ExplainFormatBinary, ExplainFormatTiDBJSON, + ExplainFormatCostTrace, } )