From e91c9027fa592a88e5abe530d8f61670c0f14a2d Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 17 Nov 2022 18:17:56 +0800 Subject: [PATCH] planner: update some UTs from cost model1 to model2 (#39210) ref pingcap/tidb#35240 --- planner/core/integration_test.go | 56 ----------------------- planner/core/plan_cost_ver2.go | 5 +- planner/core/testdata/plan_suite_out.json | 2 +- 3 files changed, 4 insertions(+), 59 deletions(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index d0cbd57f80a9e..db60c99fdf4c5 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -5795,62 +5795,6 @@ func TestIssue29503(t *testing.T) { require.Len(t, res.Rows(), 2) } -func TestIndexJoinCost(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set tidb_cost_model_version=2") - tk.MustExec(`drop table if exists t_outer, t_inner_pk, t_inner_idx`) - tk.MustExec(`create table t_outer (a int)`) - tk.MustExec(`create table t_inner_pk (a int primary key)`) - tk.MustExec(`create table t_inner_idx (a int, b int, key(a))`) - - // Default RPC encoding may cause statistics explain result differ and then the test unstable. - tk.MustExec("set @@tidb_enable_chunk_rpc = on") - - tk.MustQuery(`explain format=verbose select /*+ TIDB_INLJ(t_outer, t_inner_pk) */ * from t_outer, t_inner_pk where t_outer.a=t_inner_pk.a`).Check(testkit.Rows( // IndexJoin with inner TableScan - `IndexJoin_11 12487.50 11918182.35 root inner join, inner:TableReader_8, outer key:test.t_outer.a, inner key:test.t_inner_pk.a, equal cond:eq(test.t_outer.a, test.t_inner_pk.a)`, - `├─TableReader_18(Build) 9990.00 211131.09 root data:Selection_17`, - `│ └─Selection_17 9990.00 2534000.00 cop[tikv] not(isnull(test.t_outer.a))`, - `│ └─TableFullScan_16 10000.00 2035000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, - `└─TableReader_8(Probe) 9990.00 10.25 root data:TableRangeScan_7`, - ` └─TableRangeScan_7 9990.00 122.10 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 11918207.26 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 211131.09 root data:Selection_19`, - `│ └─Selection_19 9990.00 2534000.00 cop[tikv] not(isnull(test.t_outer.a))`, - `│ └─TableFullScan_18 10000.00 2035000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, - `└─IndexReader_9(Probe) 12487.50 23.02 root index:Selection_8`, - ` └─Selection_8 12487.50 266.14 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` └─IndexRangeScan_7 12500.00 203.70 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 11922930.69 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 211131.09 root data:Selection_22`, - `│ └─Selection_22 9990.00 2534000.00 cop[tikv] not(isnull(test.t_outer.a))`, - `│ └─TableFullScan_21 10000.00 2035000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, - `└─IndexLookUp_10(Probe) 12487.50 2443.84 root `, - ` ├─Selection_9(Build) 12487.50 317.07 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` │ └─IndexRangeScan_7 12500.00 254.63 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) 12487.50 284.13 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 11918207.26 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 211131.09 root data:Selection_19`, - `│ └─Selection_19 9990.00 2534000.00 cop[tikv] not(isnull(test.t_outer.a))`, - `│ └─TableFullScan_18 10000.00 2035000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, - `└─IndexReader_9(Probe) 12487.50 23.02 root index:Selection_8`, - ` └─Selection_8 12487.50 266.14 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` └─IndexRangeScan_7 12500.00 203.70 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 11918207.26 root inner join, inner:IndexReader_15, outer key:test.t_outer.a, inner key:test.t_inner_idx.a`, - `├─TableReader_20(Build) 9990.00 211131.09 root data:Selection_19`, - `│ └─Selection_19 9990.00 2534000.00 cop[tikv] not(isnull(test.t_outer.a))`, - `│ └─TableFullScan_18 10000.00 2035000.00 cop[tikv] table:t_outer keep order:false, stats:pseudo`, - `└─IndexReader_15(Probe) 12487.50 23.02 root index:Selection_14`, - ` └─Selection_14 12487.50 266.14 cop[tikv] not(isnull(test.t_inner_idx.a))`, - ` └─IndexRangeScan_13 12500.00 203.70 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) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/planner/core/plan_cost_ver2.go b/planner/core/plan_cost_ver2.go index 81a59e904ed41..18b1606042448 100644 --- a/planner/core/plan_cost_ver2.go +++ b/planner/core/plan_cost_ver2.go @@ -567,9 +567,10 @@ func (p *PhysicalIndexJoin) getPlanCostVer2(taskType property.TaskType, option * // `innerCostPerBatch * numberOfBatches` instead of `innerCostPerRow * numberOfOuterRow`. // Use an empirical value batchRatio to handle this now. // TODO: remove this empirical value. - batchRatio := 1024.0 + batchRatio := 1800.0 probeCost := divCostVer2(mulCostVer2(probeChildCost, buildRows), batchRatio) - doubleReadCost := doubleReadCostVer2(option, buildRows/batchRatio, requestFactor) + numTasks := math.Max(buildRows/batchRatio, 0.001) + doubleReadCost := doubleReadCostVer2(option, numTasks, requestFactor) p.planCostVer2 = sumCostVer2(buildChildCost, buildFilterCost, divCostVer2(sumCostVer2(probeCost, probeFilterCost, doubleReadCost), probeConcurrency)) p.planCostInit = true diff --git a/planner/core/testdata/plan_suite_out.json b/planner/core/testdata/plan_suite_out.json index b4da9d119f28f..a58d243b30033 100644 --- a/planner/core/testdata/plan_suite_out.json +++ b/planner/core/testdata/plan_suite_out.json @@ -546,7 +546,7 @@ }, { "SQL": "select * from t t1 join t t2 on t1.b = t2.a order by t1.a", - "Best": "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.b,test.t.a)->Sort" + "Best": "IndexJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.b,test.t.a)" }, { "SQL": "select * from t t1 join t t2 on t1.b = t2.a order by t1.a limit 1",