From 683ba092b565aa5947da998507550f8da5c406bd Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 17 May 2022 00:02:38 +0800 Subject: [PATCH] planner: enable the new cost implementation by default (#34567) close pingcap/tidb#33945 --- executor/set_test.go | 2 +- planner/core/find_best_task.go | 4 +++ planner/core/integration_test.go | 20 +++++------ .../core/testdata/integration_suite_out.json | 34 +++++++++---------- sessionctx/variable/sysvar.go | 2 +- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/executor/set_test.go b/executor/set_test.go index cdacc31b905a7..08e2a8b8b53b3 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -594,7 +594,7 @@ func TestSetVar(t *testing.T) { tk.MustQuery("show global variables like 'tidb_ignore_prepared_cache_close_stmt'").Check(testkit.Rows("tidb_ignore_prepared_cache_close_stmt OFF")) // test for tidb_enable_new_cost_interface - tk.MustQuery("select @@global.tidb_enable_new_cost_interface").Check(testkit.Rows("0")) // default value is 0 + tk.MustQuery("select @@global.tidb_enable_new_cost_interface").Check(testkit.Rows("1")) // default value is 1 tk.MustExec("set global tidb_enable_new_cost_interface=1") tk.MustQuery("select @@global.tidb_enable_new_cost_interface").Check(testkit.Rows("1")) tk.MustQuery("show global variables like 'tidb_enable_new_cost_interface'").Check(testkit.Rows()) // hidden diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index cdeb7c5b78233..c720879d15ed4 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -1821,6 +1821,8 @@ func (s *LogicalTableScan) GetPhysicalScan(schema *expression.Schema, stats *pro physicalTableID: ds.physicalTableID, Ranges: s.Ranges, AccessCondition: s.AccessConds, + tblCols: ds.TblCols, + tblColHists: ds.TblColHists, }.Init(s.ctx, s.blockOffset) ts.stats = stats ts.SetSchema(schema.Clone()) @@ -1850,6 +1852,8 @@ func (s *LogicalIndexScan) GetPhysicalIndexScan(schema *expression.Schema, stats dataSourceSchema: ds.schema, isPartition: ds.isPartition, physicalTableID: ds.physicalTableID, + tblColHists: ds.TblColHists, + pkIsHandleCol: ds.getPKIsHandleCol(), }.Init(ds.ctx, ds.blockOffset) is.stats = stats is.initSchema(s.FullIdxCols, s.IsDoubleRead) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 1c4b1675b0869..00e9cab055693 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -5398,24 +5398,24 @@ func TestIndexJoinCost(t *testing.T) { `│ └─Selection_17 9990.00 465000.00 cop[tikv] not(isnull(test.t_outer.a))`, `│ └─TableFullScan_16 10000.00 435000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, `└─TableReader_8(Probe) 1.00 3.88 root data:TableRangeScan_7`, - ` └─TableRangeScan_7 1.00 0.00 cop[tikv] table:t_inner_pk range: decided by [test.t_outer.a], keep order:false, stats:pseudo`)) + ` └─TableRangeScan_7 1.00 30.00 cop[tikv] table:t_inner_pk range: decided by [test.t_outer.a], keep order:false, stats:pseudo`)) tk.MustQuery(`explain format=verbose select /*+ TIDB_INLJ(t_outer, t_inner_idx) */ t_inner_idx.a from t_outer, t_inner_idx where t_outer.a=t_inner_idx.a`).Check(testkit.Rows( // IndexJoin with inner IndexScan `IndexJoin_10 12487.50 235192.19 root inner join, inner:IndexReader_9, outer key:test.t_outer.a, inner key:test.t_inner_idx.a, equal cond:eq(test.t_outer.a, test.t_inner_idx.a)`, `├─TableReader_20(Build) 9990.00 36412.58 root data:Selection_19`, `│ └─Selection_19 9990.00 465000.00 cop[tikv] not(isnull(test.t_outer.a))`, `│ └─TableFullScan_18 10000.00 435000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, `└─IndexReader_9(Probe) 1.25 5.89 root index:Selection_8`, - ` └─Selection_8 1.25 0.00 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` └─IndexRangeScan_7 1.25 0.00 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:false, stats:pseudo`)) + ` └─Selection_8 1.25 58.18 cop[tikv] not(isnull(test.t_inner_idx.a))`, + ` └─IndexRangeScan_7 1.25 54.43 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:false, stats:pseudo`)) tk.MustQuery(`explain format=verbose select /*+ TIDB_INLJ(t_outer, t_inner_idx) */ * from t_outer, t_inner_idx where t_outer.a=t_inner_idx.a`).Check(testkit.Rows( // IndexJoin with inner IndexLookup `IndexJoin_11 12487.50 531469.38 root inner join, inner:IndexLookUp_10, outer key:test.t_outer.a, inner key:test.t_inner_idx.a, equal cond:eq(test.t_outer.a, test.t_inner_idx.a)`, `├─TableReader_23(Build) 9990.00 36412.58 root data:Selection_22`, `│ └─Selection_22 9990.00 465000.00 cop[tikv] not(isnull(test.t_outer.a))`, `│ └─TableFullScan_21 10000.00 435000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, `└─IndexLookUp_10(Probe) 1.25 35.55 root `, - ` ├─Selection_9(Build) 1.25 0.00 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` │ └─IndexRangeScan_7 1.25 0.00 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:false, stats:pseudo`, - ` └─TableRowIDScan_8(Probe) 1.25 0.00 cop[tikv] table:t_inner_idx keep order:false, stats:pseudo`)) + ` ├─Selection_9(Build) 1.25 75.08 cop[tikv] not(isnull(test.t_inner_idx.a))`, + ` │ └─IndexRangeScan_7 1.25 71.32 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:false, stats:pseudo`, + ` └─TableRowIDScan_8(Probe) 1.25 71.25 cop[tikv] table:t_inner_idx keep order:false, stats:pseudo`)) tk.MustQuery("explain format=verbose select /*+ inl_hash_join(t_outer, t_inner_idx) */ t_inner_idx.a from t_outer, t_inner_idx where t_outer.a=t_inner_idx.a").Check(testkit.Rows( `IndexHashJoin_12 12487.50 235192.19 root inner join, inner:IndexReader_9, outer key:test.t_outer.a, inner key:test.t_inner_idx.a, equal cond:eq(test.t_outer.a, test.t_inner_idx.a)`, @@ -5423,16 +5423,16 @@ func TestIndexJoinCost(t *testing.T) { `│ └─Selection_19 9990.00 465000.00 cop[tikv] not(isnull(test.t_outer.a))`, `│ └─TableFullScan_18 10000.00 435000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, `└─IndexReader_9(Probe) 1.25 5.89 root index:Selection_8`, - ` └─Selection_8 1.25 0.00 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` └─IndexRangeScan_7 1.25 0.00 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:false, stats:pseudo`)) + ` └─Selection_8 1.25 58.18 cop[tikv] not(isnull(test.t_inner_idx.a))`, + ` └─IndexRangeScan_7 1.25 54.43 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:false, stats:pseudo`)) tk.MustQuery("explain format=verbose select /*+ inl_merge_join(t_outer, t_inner_idx) */ t_inner_idx.a from t_outer, t_inner_idx where t_outer.a=t_inner_idx.a").Check(testkit.Rows( `IndexMergeJoin_17 12487.50 229210.68 root inner join, inner:IndexReader_15, outer key:test.t_outer.a, inner key:test.t_inner_idx.a`, `├─TableReader_20(Build) 9990.00 36412.58 root data:Selection_19`, `│ └─Selection_19 9990.00 465000.00 cop[tikv] not(isnull(test.t_outer.a))`, `│ └─TableFullScan_18 10000.00 435000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, `└─IndexReader_15(Probe) 1.25 5.89 root index:Selection_14`, - ` └─Selection_14 1.25 0.00 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` └─IndexRangeScan_13 1.25 0.00 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:true, stats:pseudo`)) + ` └─Selection_14 1.25 58.18 cop[tikv] not(isnull(test.t_inner_idx.a))`, + ` └─IndexRangeScan_13 1.25 54.43 cop[tikv] table:t_inner_idx, index:a(a) range: decided by [eq(test.t_inner_idx.a, test.t_outer.a)], keep order:true, stats:pseudo`)) } func TestHeuristicIndexSelection(t *testing.T) { diff --git a/planner/core/testdata/integration_suite_out.json b/planner/core/testdata/integration_suite_out.json index f3f200872b93e..fdb3bca061701 100644 --- a/planner/core/testdata/integration_suite_out.json +++ b/planner/core/testdata/integration_suite_out.json @@ -1723,7 +1723,7 @@ "SQL": "select a, b, c from t2 where a = 1 and b = 2 and c in (1, 2, 3, 4, 5)", "Plan": [ "Selection_6 0.01 8.60 root eq(test.t2.b, 2), in(test.t2.c, 1, 2, 3, 4, 5)", - "└─Point_Get_5 1.00 0.00 root table:t2, index:idx_a(a) " + "└─Point_Get_5 1.00 5.60 root table:t2, index:idx_a(a) " ], "Warnings": [ "Note 1105 unique index idx_a of t2 is selected since the path only has point ranges with double scan" @@ -1780,7 +1780,7 @@ "Plan": [ "Delete_4 N/A N/A root N/A", "└─Selection_7 2.00 9.80 root in(test.t1.g, 3, 4)", - " └─Point_Get_6 1.00 0.00 root table:t1, index:f(f) " + " └─Point_Get_6 1.00 6.80 root table:t1, index:f(f) " ], "Warnings": [ "Note 1105 unique index f of t1 is selected since the path only has point ranges with double scan" @@ -1816,9 +1816,9 @@ "SQL": "select * from t where a > 1 order by f", "Plan": [ "IndexLookUp_14 3333.33 136747.00 root ", - "├─Selection_13(Build) 3333.33 0.00 cop[tikv] gt(test.t.a, 1)", + "├─Selection_13(Build) 3333.33 585000.00 cop[tikv] gt(test.t.a, 1)", "│ └─IndexFullScan_11 10000.00 555000.00 cop[tikv] table:t, index:f(f) keep order:true, stats:pseudo", - "└─TableRowIDScan_12(Probe) 3333.33 555000.00 cop[tikv] table:t keep order:false, stats:pseudo" + "└─TableRowIDScan_12(Probe) 3333.33 370000.00 cop[tikv] table:t keep order:false, stats:pseudo" ], "Warnings": [ "Note 1105 [t,f,f_g] remain after pruning paths for t given Prop{SortItems: [{test.t.f asc}], TaskTp: rootTask}" @@ -1829,7 +1829,7 @@ "Plan": [ "IndexLookUp_10 3333.33 86674.83 root ", "├─IndexRangeScan_8(Build) 3333.33 185000.00 cop[tikv] table:t, index:f(f) range:(1,+inf], keep order:false, stats:pseudo", - "└─TableRowIDScan_9(Probe) 3333.33 185000.00 cop[tikv] table:t keep order:false, stats:pseudo" + "└─TableRowIDScan_9(Probe) 3333.33 370000.00 cop[tikv] table:t keep order:false, stats:pseudo" ], "Warnings": [ "Note 1105 [t,f,f_g] remain after pruning paths for t given Prop{SortItems: [], TaskTp: rootTask}" @@ -1850,8 +1850,8 @@ "Plan": [ "IndexLookUp_15 3.33 206.74 root ", "├─IndexRangeScan_12(Build) 10.00 570.00 cop[tikv] table:t, index:g(g) range:[5,5], keep order:false, stats:pseudo", - "└─Selection_14(Probe) 3.33 0.00 cop[tikv] gt(test.t.f, 3)", - " └─TableRowIDScan_13 10.00 570.00 cop[tikv] table:t keep order:false, stats:pseudo" + "└─Selection_14(Probe) 3.33 1140.00 cop[tikv] gt(test.t.f, 3)", + " └─TableRowIDScan_13 10.00 1110.00 cop[tikv] table:t keep order:false, stats:pseudo" ], "Warnings": [ "Note 1105 [t,f_g,g] remain after pruning paths for t given Prop{SortItems: [], TaskTp: rootTask}" @@ -1863,7 +1863,7 @@ "Sort_5 10.00 353.68 root test.t.f", "└─IndexLookUp_13 10.00 230.01 root ", " ├─IndexRangeScan_11(Build) 10.00 570.00 cop[tikv] table:t, index:g(g) range:[5,5], keep order:false, stats:pseudo", - " └─TableRowIDScan_12(Probe) 10.00 570.00 cop[tikv] table:t keep order:false, stats:pseudo" + " └─TableRowIDScan_12(Probe) 10.00 1110.00 cop[tikv] table:t keep order:false, stats:pseudo" ], "Warnings": [ "Note 1105 [t,g] remain after pruning paths for t given Prop{SortItems: [], TaskTp: rootTask}" @@ -1873,9 +1873,9 @@ "SQL": "select * from t where d = 3 order by c, e", "Plan": [ "IndexLookUp_15 10.00 57222.78 root ", - "├─Selection_14(Build) 10.00 0.00 cop[tikv] eq(test.t.d, 3)", + "├─Selection_14(Build) 10.00 855000.00 cop[tikv] eq(test.t.d, 3)", "│ └─IndexFullScan_12 10000.00 825000.00 cop[tikv] table:t, index:c_d_e(c, d, e) keep order:true, stats:pseudo", - "└─TableRowIDScan_13(Probe) 10.00 825000.00 cop[tikv] table:t keep order:false, stats:pseudo" + "└─TableRowIDScan_13(Probe) 10.00 1110.00 cop[tikv] table:t keep order:false, stats:pseudo" ], "Warnings": [ "Note 1105 [t,c_d_e] remain after pruning paths for t given Prop{SortItems: [{test.t.c asc} {test.t.e asc}], TaskTp: rootTask}" @@ -1932,7 +1932,7 @@ "Plan": [ "IndexLookUp_7 3.00 57.91 root ", "├─IndexRangeScan_5(Build) 3.00 171.00 cop[tikv] table:t, index:idx_b(b) range:(5,+inf], keep order:false", - "└─TableRowIDScan_6(Probe) 3.00 171.00 cop[tikv] table:t keep order:false" + "└─TableRowIDScan_6(Probe) 3.00 108.00 cop[tikv] table:t keep order:false" ], "Warnings": [ "Note 1105 [idx_b] remain after pruning paths for t given Prop{SortItems: [], TaskTp: rootTask}" @@ -2355,7 +2355,7 @@ { "SQL": "explain format = 'verbose' select * from t3 order by a", "Plan": [ - "Sort_4 3.00 45.85 root test.t3.a", + "Sort_4 3.00 26.05 root test.t3.a", "└─TableReader_8 3.00 11.78 root data:TableFullScan_7", " └─TableFullScan_7 3.00 108.00 cop[tikv] table:t3 keep order:false" ] @@ -2373,7 +2373,7 @@ "Plan": [ "TopN_7 1.00 13.22 root test.t3.a, offset:0, count:1", "└─TableReader_16 1.00 10.22 root data:TopN_15", - " └─TopN_15 1.00 0.00 cop[tikv] test.t3.a, offset:0, count:1", + " └─TopN_15 1.00 117.00 cop[tikv] test.t3.a, offset:0, count:1", " └─TableFullScan_14 3.00 108.00 cop[tikv] table:t3 keep order:false" ] }, @@ -2382,7 +2382,7 @@ "Plan": [ "TopN_7 1.00 13.22 root test.t3.b, offset:0, count:1", "└─TableReader_16 1.00 10.22 root data:TopN_15", - " └─TopN_15 1.00 0.00 cop[tikv] test.t3.b, offset:0, count:1", + " └─TopN_15 1.00 117.00 cop[tikv] test.t3.b, offset:0, count:1", " └─TableFullScan_14 3.00 108.00 cop[tikv] table:t3 keep order:false" ] }, @@ -2394,7 +2394,7 @@ " └─Projection_22 3.00 76.80 mpp[tiflash] Column#4", " └─HashAgg_8 3.00 57.00 mpp[tiflash] group by:test.t2.a, funcs:count(1)->Column#4", " └─ExchangeReceiver_21 3.00 48.00 mpp[tiflash] ", - " └─ExchangeSender_20 3.00 48.00 mpp[tiflash] ExchangeType: HashPartition, Hash Cols: [name: test.t2.a, collate: binary]", + " └─ExchangeSender_20 3.00 45.00 mpp[tiflash] ExchangeType: HashPartition, Hash Cols: [name: test.t2.a, collate: binary]", " └─TableFullScan_19 3.00 45.00 mpp[tiflash] table:t2 keep order:false" ] }, @@ -2444,7 +2444,7 @@ " └─ExchangeSender_45 3.00 195.38 mpp[tiflash] ExchangeType: PassThrough", " └─HashJoin_42 3.00 195.38 mpp[tiflash] inner join, equal:[eq(test.t1.a, test.t2.a)]", " ├─ExchangeReceiver_21(Build) 3.00 57.00 mpp[tiflash] ", - " │ └─ExchangeSender_20 3.00 57.00 mpp[tiflash] ExchangeType: Broadcast", + " │ └─ExchangeSender_20 3.00 54.00 mpp[tiflash] ExchangeType: Broadcast", " │ └─Selection_19 3.00 54.00 mpp[tiflash] not(isnull(test.t1.a))", " │ └─TableFullScan_18 3.00 45.00 mpp[tiflash] table:t1 keep order:false", " └─Selection_23(Probe) 3.00 54.00 mpp[tiflash] not(isnull(test.t2.a))", @@ -2462,7 +2462,7 @@ " └─ExchangeSender_38 3.00 204.38 mpp[tiflash] ExchangeType: PassThrough", " └─HashJoin_29 3.00 204.38 mpp[tiflash] inner join, equal:[eq(test.t1.a, test.t2.a)]", " ├─ExchangeReceiver_35(Build) 3.00 66.00 mpp[tiflash] ", - " │ └─ExchangeSender_34 3.00 66.00 mpp[tiflash] ExchangeType: Broadcast", + " │ └─ExchangeSender_34 3.00 63.00 mpp[tiflash] ExchangeType: Broadcast", " │ └─Selection_33 3.00 63.00 mpp[tiflash] not(isnull(test.t1.a)), not(isnull(test.t1.b))", " │ └─TableFullScan_32 3.00 54.00 mpp[tiflash] table:t1 keep order:false", " └─Selection_37(Probe) 3.00 54.00 mpp[tiflash] not(isnull(test.t2.a))", diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 68207dc0588b0..ca9b52493d53e 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -1455,7 +1455,7 @@ var defaultSysVars = []*SysVar{ return nil }, }, - {Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableNewCostInterface, Value: BoolToOnOff(false), Hidden: true, Type: TypeBool, + {Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableNewCostInterface, Value: BoolToOnOff(true), Hidden: true, Type: TypeBool, SetSession: func(vars *SessionVars, s string) error { vars.EnableNewCostInterface = TiDBOptOn(s) return nil