Skip to content

Commit

Permalink
squash! merge pingcap#14831, add implementation rule for IndexLoopUpJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
francis0407 committed Apr 7, 2020
1 parent cf72748 commit 8b28df4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 31 deletions.
108 changes: 81 additions & 27 deletions planner/cascades/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@
{
"SQL": "select t1.a, t1.b from t as t1 left join t as t2 on t1.a = t2.a and t1.b = 3 order by a",
"Plan": [
"TableReader_24 4.00 root data:TableFullScan_25",
"└─TableFullScan_25 4.00 cop[tikv] table:t1 pk col:test.t.a, keep order:true"
"TableReader_27 4.00 root data:TableFullScan_28",
"└─TableFullScan_28 4.00 cop[tikv] table:t1 pk col:test.t.a, keep order:true"
],
"Result": [
"1 11",
Expand Down Expand Up @@ -879,10 +879,10 @@
"Plan": [
"Projection_16 2.00 root test.t1.a, test.t1.b",
"└─MergeJoin_19 2.00 root inner join, left key:test.t1.a, right key:test.t2.a",
" ├─TableReader_27(Build) 2.00 root data:TableRangeScan_28",
" │ └─TableRangeScan_28 2.00 cop[tikv] table:t2 pk col:test.t2.a, range:(2,+inf], keep order:true",
" └─TableReader_24(Probe) 2.00 root data:TableRangeScan_25",
" └─TableRangeScan_25 2.00 cop[tikv] table:t1 pk col:test.t1.a, range:(2,+inf], keep order:true"
" ├─TableReader_35(Build) 2.00 root data:TableRangeScan_36",
" │ └─TableRangeScan_36 2.00 cop[tikv] table:t2 pk col:test.t2.a, range:(2,+inf], keep order:true",
" └─TableReader_32(Probe) 2.00 root data:TableRangeScan_33",
" └─TableRangeScan_33 2.00 cop[tikv] table:t1 pk col:test.t1.a, range:(2,+inf], keep order:true"
],
"Result": [
"3 33"
Expand Down Expand Up @@ -911,10 +911,10 @@
"Projection_17 1.60 root test.t1.a, test.t1.b",
"└─Selection_18 1.60 root gt(test.t2.b, 200)",
" └─MergeJoin_21 2.00 root left outer join, left key:test.t1.a, right key:test.t2.a",
" ├─TableReader_29(Build) 2.00 root data:TableRangeScan_30",
" │ └─TableRangeScan_30 2.00 cop[tikv] table:t2 pk col:test.t2.a, range:(2,+inf], keep order:true",
" └─TableReader_26(Probe) 2.00 root data:TableRangeScan_27",
" └─TableRangeScan_27 2.00 cop[tikv] table:t1 pk col:test.t1.a, range:(2,+inf], keep order:true"
" ├─TableReader_33(Build) 2.00 root data:TableRangeScan_34",
" │ └─TableRangeScan_34 2.00 cop[tikv] table:t2 pk col:test.t2.a, range:(2,+inf], keep order:true",
" └─TableReader_30(Probe) 2.00 root data:TableRangeScan_31",
" └─TableRangeScan_31 2.00 cop[tikv] table:t1 pk col:test.t1.a, range:(2,+inf], keep order:true"
],
"Result": [
"3 33"
Expand All @@ -926,11 +926,11 @@
"Projection_14 1.07 root test.t2.a, test.t2.b",
"└─Selection_15 1.07 root gt(test.t1.a, 2)",
" └─MergeJoin_18 1.33 root right outer join, left key:test.t1.a, right key:test.t2.a",
" ├─TableReader_24(Build) 4.00 root data:TableFullScan_25",
" │ └─TableFullScan_25 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true",
" └─TableReader_27(Probe) 1.33 root data:Selection_29",
" └─Selection_29 1.33 cop[tikv] gt(test.t2.b, 200)",
" └─TableFullScan_28 4.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true"
" ├─TableReader_27(Build) 4.00 root data:TableFullScan_28",
" │ └─TableFullScan_28 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true",
" └─TableReader_30(Probe) 1.33 root data:Selection_32",
" └─Selection_32 1.33 cop[tikv] gt(test.t2.b, 200)",
" └─TableFullScan_31 4.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true"
],
"Result": [
"3 333"
Expand All @@ -941,10 +941,10 @@
"Plan": [
"Projection_12 4.00 root test.t1.a, test.t1.b",
"└─MergeJoin_13 4.00 root inner join, left key:test.t1.a, right key:test.t2.a",
" ├─TableReader_19(Build) 4.00 root data:TableFullScan_20",
" │ └─TableFullScan_20 4.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true",
" └─TableReader_14(Probe) 4.00 root data:TableFullScan_15",
" └─TableFullScan_15 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true"
" ├─TableReader_22(Build) 4.00 root data:TableFullScan_23",
" │ └─TableFullScan_23 4.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true",
" └─TableReader_17(Probe) 4.00 root data:TableFullScan_18",
" └─TableFullScan_18 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true"
],
"Result": [
"1 11",
Expand All @@ -956,10 +956,10 @@
"SQL": "select * from t1 join t2 on t1.a = t2.a",
"Plan": [
"MergeJoin_11 4.00 root inner join, left key:test.t1.a, right key:test.t2.a",
"├─TableReader_19(Build) 4.00 root data:TableFullScan_20",
"│ └─TableFullScan_20 4.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true",
"└─TableReader_16(Probe) 4.00 root data:TableFullScan_17",
" └─TableFullScan_17 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true"
"├─TableReader_25(Build) 4.00 root data:TableFullScan_26",
"│ └─TableFullScan_26 4.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true",
"└─TableReader_22(Probe) 4.00 root data:TableFullScan_23",
" └─TableFullScan_23 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true"
],
"Result": [
"1 11 1 111",
Expand Down Expand Up @@ -1090,10 +1090,10 @@
"SQL": "select a from t1 where exists(select 1 from t2 where t1.a = t2.a)",
"Plan": [
"MergeJoin_28 4.00 root semi join, left key:test.t1.a, right key:test.t2.a",
"├─TableReader_34(Build) 3.00 root data:TableFullScan_35",
"│ └─TableFullScan_35 3.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true",
"└─TableReader_31(Probe) 4.00 root data:TableFullScan_32",
" └─TableFullScan_32 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true"
"├─TableReader_37(Build) 3.00 root data:TableFullScan_38",
"│ └─TableFullScan_38 3.00 cop[tikv] table:t2 pk col:test.t2.a, keep order:true",
"└─TableReader_34(Probe) 4.00 root data:TableFullScan_35",
" └─TableFullScan_35 4.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true"
],
"Result": [
"1",
Expand Down Expand Up @@ -1419,5 +1419,59 @@
]
}
]
},
{
"Name": "TestIndexJoin",
"Cases": [
{
"SQL": "select * from t1 left join t2 on t1.b = t2.a order by t1.a",
"Plan": [
"IndexJoin_13 12500.00 root left outer join, inner:TableReader_12, outer key:test.t1.b, inner key:test.t2.a",
"├─TableReader_14(Build) 10000.00 root data:TableFullScan_15",
"│ └─TableFullScan_15 10000.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true, stats:pseudo",
"└─TableReader_12(Probe) 1.00 root data:TableRangeScan_11",
" └─TableRangeScan_11 1.00 cop[tikv] table:t2 , range: decided by [test.t1.b]keep order:false, stats:pseudo"
],
"Result": [
"0 0 0 111",
"1 0 0 111",
"2 0 0 111",
"3 0 0 111"
]
},
{
"SQL": "select * from t1 left join t2 on t1.b = t2.a and t1.b = t2.b and t1.a > 10 order by t1.a",
"Plan": [
"IndexJoin_13 12500.00 root left outer join, inner:TableReader_12, outer key:test.t1.b, inner key:test.t2.a, left cond:gt(test.t1.a, 10), other cond:eq(test.t1.b, test.t2.b)",
"├─TableReader_14(Build) 10000.00 root data:TableFullScan_15",
"│ └─TableFullScan_15 10000.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true, stats:pseudo",
"└─TableReader_12(Probe) 1.00 root data:TableRangeScan_11",
" └─TableRangeScan_11 1.00 cop[tikv] table:t2 , range: decided by [test.t1.b test.t1.b]keep order:false, stats:pseudo"
],
"Result": [
"0 0 <nil> <nil>",
"1 0 <nil> <nil>",
"2 0 <nil> <nil>",
"3 0 <nil> <nil>"
]
},
{
"SQL": "select * from t1 left join (select * from t2 where t2.a > 0 and t2.b > 0) as t3 on t1.b = t3.a order by t1.a",
"Plan": [
"IndexJoin_18 10000.00 root left outer join, inner:TableReader_17, outer key:test.t1.b, inner key:test.t2.a",
"├─TableReader_19(Build) 10000.00 root data:TableFullScan_20",
"│ └─TableFullScan_20 10000.00 cop[tikv] table:t1 pk col:test.t1.a, keep order:true, stats:pseudo",
"└─TableReader_17(Probe) 0.14 root data:Selection_16",
" └─Selection_16 0.14 cop[tikv] gt(test.t2.a, 0)",
" └─TableRangeScan_15 0.42 cop[tikv] table:t2 , range: decided by [test.t1.b]keep order:false, stats:pseudo"
],
"Result": [
"0 0 <nil> <nil>",
"1 0 <nil> <nil>",
"2 0 <nil> <nil>",
"3 0 <nil> <nil>"
]
}
]
}
]
4 changes: 2 additions & 2 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ func (s *LogicalTableScan) GetPhysicalScan(schema *expression.Schema, stats *pro
rowCount = math.Min(prop.ExpectedCnt/selectivity, rowCount)
}
// TODO: Estimate row count using ds.crossEstimateRowCount().
ts.stats = ds.tableStats.ScaleByExpectCnt(rowCount)
ts.stats = ds.TableStats.ScaleByExpectCnt(rowCount)
if len(s.FilterConds) == 0 {
return ts, nil
}
Expand Down Expand Up @@ -1093,7 +1093,7 @@ func (s *LogicalIndexScan) GetPhysicalIndexScan(schema *expression.Schema, stats
selectivity := stats.RowCount / s.CountAfterAccess
rowCount = math.Min(prop.ExpectedCnt/selectivity, rowCount)
}
is.stats = ds.tableStats.ScaleByExpectCnt(rowCount)
is.stats = ds.TableStats.ScaleByExpectCnt(rowCount)
if len(s.FilterConds) == 0 {
return is, nil
}
Expand Down
4 changes: 2 additions & 2 deletions planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (ts *LogicalTableScan) DeriveStats(childStats []*property.StatsInfo, selfSc
}
}
ts.Ranges = ranger.FullIntRange(isUnsigned)
ts.CountAfterAccess = ts.Source.tableStats.RowCount
ts.CountAfterAccess = ts.Source.TableStats.RowCount
ts.FilterConds = ts.AllConds
}
return ts.stats, nil
Expand Down Expand Up @@ -332,7 +332,7 @@ func (is *LogicalIndexScan) DeriveStats(childStats []*property.StatsInfo, selfSc
is.EqCondCount = res.EqCondCount
is.EqOrInCondCount = res.EqOrInCount
// TODO: `res` still has an unused field: IsDNFCond.
is.CountAfterAccess, err = is.Source.tableStats.HistColl.GetRowCountByIndexRanges(is.SCtx().GetSessionVars().StmtCtx, is.Index.ID, is.Ranges)
is.CountAfterAccess, err = is.Source.TableStats.HistColl.GetRowCountByIndexRanges(is.SCtx().GetSessionVars().StmtCtx, is.Index.ID, is.Ranges)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8b28df4

Please sign in to comment.