Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: update some UTs from cost model1 to model2 #39210

Merged
merged 8 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions planner/core/plan_cost_ver2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down