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

*: add stats version in slow_query and slow log. #10082

Merged
merged 24 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fd348fe
planer: add stats version in basePlan.StatsInfo
crazycs520 Apr 9, 2019
6c7297c
*: add stats info to slow_query table and slow log
crazycs520 Apr 9, 2019
edcf7fb
fix collect plan stats bug
crazycs520 Apr 9, 2019
b7110d2
add comment
crazycs520 Apr 9, 2019
10009a0
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 9, 2019
3b1b08a
fix ci and address comment
crazycs520 Apr 9, 2019
7d2430c
Merge branch 'slow-log-stats-version' of https://github.com/crazycs52…
crazycs520 Apr 9, 2019
08278c9
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 9, 2019
8690f15
address comment
crazycs520 Apr 9, 2019
c661f47
Update planner/property/stats_info.go
zz-jason Apr 10, 2019
73f1dd3
Update sessionctx/variable/session.go
zz-jason Apr 10, 2019
ec2c796
fix cycle reference
crazycs520 Apr 10, 2019
eaeaa81
address comment
crazycs520 Apr 10, 2019
49acf20
fix test
crazycs520 Apr 10, 2019
f2179e4
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 10, 2019
403defb
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 10, 2019
07328a7
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 11, 2019
8667b11
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 11, 2019
1f44254
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 11, 2019
75b2518
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 12, 2019
4070a4b
Merge branch 'master' of https://github.com/pingcap/tidb into slow-lo…
crazycs520 Apr 12, 2019
6f84553
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 12, 2019
0a0aa43
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 15, 2019
3536abb
Merge branch 'master' into slow-log-stats-version
crazycs520 Apr 15, 2019
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
8 changes: 4 additions & 4 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ IndexJoin_12 4166.67 root left outer join, inner:IndexLookUp_11, outer key:test.
└─IndexLookUp_11 0.00 root
├─Selection_10 0.00 cop not(isnull(test.t2.c1))
│ └─IndexScan_8 10.00 cop table:t2, index:c1, range: decided by [test.t1.c2], keep order:false, stats:pseudo
└─TableScan_9 0.00 cop table:t2, keep order:false
└─TableScan_9 0.00 cop table:t2, keep order:false, stats:pseudo
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
id count task operator info
Point_Get_1 1.00 root table:t1, handle:1
Expand Down Expand Up @@ -80,7 +80,7 @@ IndexLookUp_12 1.11 root
├─Selection_10 3.33 cop gt(test.t1.c1, 1)
│ └─IndexScan_8 10.00 cop table:t1, index:c2, range:[1,1], keep order:false, stats:pseudo
└─Selection_11 1.11 cop lt(test.t1.c3, 1)
└─TableScan_9 3.33 cop table:t1, keep order:false
└─TableScan_9 3.33 cop table:t1, keep order:false, stats:pseudo
explain select * from t1 where c1 = 1 and c2 > 1;
id count task operator info
TableReader_7 0.33 root data:Selection_6
Expand Down Expand Up @@ -130,7 +130,7 @@ Projection_12 10000.00 root eq(test.t1.c2, test.t2.c2)
└─IndexLookUp_40 1.00 root
├─Limit_39 1.00 cop offset:0, count:1
│ └─IndexScan_37 1.00 cop table:t2, index:c1, range: decided by [eq(test.t1.c1, test.t2.c1)], keep order:true, stats:pseudo
└─TableScan_38 1.00 cop table:t2, keep order:false
└─TableScan_38 1.00 cop table:t2, keep order:false, stats:pseudo
explain select * from t1 order by c1 desc limit 1;
id count task operator info
Limit_10 1.00 root offset:0, count:1
Expand All @@ -145,7 +145,7 @@ Limit_9 1.00 root offset:0, count:1
│ └─IndexScan_11 9.00 cop table:t4, index:a, b, range:(1,+inf], keep order:false, stats:pseudo
└─Limit_15 1.00 cop offset:0, count:1
└─Selection_14 1.00 cop gt(test.t4.c, 1)
└─TableScan_12 3.00 cop table:t4, keep order:false
└─TableScan_12 3.00 cop table:t4, keep order:false, stats:pseudo
explain select * from t4 where a > 1 and c > 1 limit 1;
id count task operator info
Limit_8 1.00 root offset:0, count:1
Expand Down
10 changes: 5 additions & 5 deletions cmd/explaintest/r/explain_easy_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ MergeJoin_7 2481.25 root left outer join, left key:test.t1.c2, right key:test.t2
├─IndexLookUp_17 1998.00 root
│ ├─Selection_16 1998.00 cop gt(test.t1.c1, 1)
│ │ └─IndexScan_14 1999.00 cop table:t1, index:c2, range:[NULL,+inf], keep order:true
│ └─TableScan_15 1998.00 cop table:t1, keep order:false
│ └─TableScan_15 1998.00 cop table:t1, keep order:false, stats:pseudo
└─Projection_22 1985.00 root test.t2.c1, test.t2.c2
└─IndexLookUp_21 1985.00 root
├─IndexScan_19 1985.00 cop table:t2, index:c1, range:[-inf,+inf], keep order:true
Expand Down Expand Up @@ -85,7 +85,7 @@ IndexLookUp_12 0.00 root
├─Selection_10 0.00 cop gt(test.t1.c1, 1)
│ └─IndexScan_8 0.00 cop table:t1, index:c2, range:[1,1], keep order:false
└─Selection_11 0.00 cop lt(test.t1.c3, 1)
└─TableScan_9 0.00 cop table:t1, keep order:false
└─TableScan_9 0.00 cop table:t1, keep order:false, stats:pseudo
explain select * from t1 where c1 = 1 and c2 > 1;
id count task operator info
TableReader_7 0.50 root data:Selection_6
Expand Down Expand Up @@ -116,7 +116,7 @@ Projection_12 1999.00 root eq(test.t1.c2, test.t2.c2)
└─IndexLookUp_40 1.00 root
├─Limit_39 1.00 cop offset:0, count:1
│ └─IndexScan_37 1.25 cop table:t2, index:c1, range: decided by [eq(test.t1.c1, test.t2.c1)], keep order:true
└─TableScan_38 1.00 cop table:t2, keep order:false
└─TableScan_38 1.00 cop table:t2, keep order:false, stats:pseudo
explain select * from t1 order by c1 desc limit 1;
id count task operator info
Limit_10 1.00 root offset:0, count:1
Expand Down Expand Up @@ -173,14 +173,14 @@ Limit_9 1.00 root offset:1, count:1
└─IndexLookUp_14 1.00 root
├─Limit_13 1.00 cop offset:0, count:2
│ └─IndexScan_11 1.00 cop table:index_prune, index:a, b, range:[1010010404050976781 26467085526790,1010010404050976781 26467085526790], keep order:false
└─TableScan_12 1.00 cop table:index_prune, keep order:false
└─TableScan_12 1.00 cop table:index_prune, keep order:false, stats:pseudo
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 LIMIT 1, 0;
id count task operator info
Limit_9 0.00 root offset:1, count:0
└─IndexLookUp_14 0.00 root
├─Limit_13 0.00 cop offset:0, count:1
│ └─IndexScan_11 1.00 cop table:index_prune, index:a, b, range:[1010010404050976781 26467085526790,1010010404050976781 26467085526790], keep order:false
└─TableScan_12 0.00 cop table:index_prune, keep order:false
└─TableScan_12 0.00 cop table:index_prune, keep order:false, stats:pseudo
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 LIMIT 0, 1;
id count task operator info
Point_Get_1 1.00 root table:index_prune, index:a b
Expand Down
4 changes: 2 additions & 2 deletions cmd/explaintest/r/index_join.result
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ IndexJoin_16 5.00 root inner join, inner:IndexLookUp_15, outer key:test.t2.a, in
├─IndexLookUp_15 0.00 root
│ ├─Selection_14 0.00 cop not(isnull(test.t1.a))
│ │ └─IndexScan_12 5.00 cop table:t1, index:a, range: decided by [test.t2.a], keep order:false
│ └─TableScan_13 0.00 cop table:t1, keep order:false
│ └─TableScan_13 0.00 cop table:t1, keep order:false, stats:pseudo
└─TableReader_19 1.00 root data:Selection_18
└─Selection_18 1.00 cop not(isnull(test.t2.a))
└─TableScan_17 1.00 cop table:t2, range:[-inf,+inf], keep order:false
Expand All @@ -24,4 +24,4 @@ Projection_6 5.00 root test.t1.a, test.t1.b, test.t2.a, test.t2.b
└─IndexLookUp_11 0.00 root
├─Selection_10 0.00 cop not(isnull(test.t1.a))
│ └─IndexScan_8 5.00 cop table:t1, index:a, range: decided by [test.t2.a], keep order:false
└─TableScan_9 0.00 cop table:t1, keep order:false
└─TableScan_9 0.00 cop table:t1, keep order:false, stats:pseudo
2 changes: 1 addition & 1 deletion cmd/explaintest/r/topn_push_down.result
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Projection_13 0.00 root te.expect_time
│ │ ├─Selection_72 0.00 cop eq(tr.business_type, 18), in(tr.trade_type, 1)
│ │ │ └─IndexScan_70 10.00 cop table:tr, index:shop_identy, trade_status, business_type, trade_pay_status, trade_type, delivery_type, source, biz_date, range:[810094178,810094178], keep order:false, stats:pseudo
│ │ └─Selection_73 0.00 cop eq(tr.brand_identy, 32314), eq(tr.domain_type, 2)
│ │ └─TableScan_71 0.00 cop table:tr, keep order:false
│ │ └─TableScan_71 0.00 cop table:tr, keep order:false, stats:pseudo
│ └─IndexLookUp_35 250.00 root
│ ├─IndexScan_32 10.00 cop table:te, index:trade_id, range: decided by [tr.id], keep order:false, stats:pseudo
│ └─Selection_34 250.00 cop ge(te.expect_time, 2018-04-23 00:00:00.000000), le(te.expect_time, 2018-04-23 23:59:59.000000)
Expand Down
27 changes: 25 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,13 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
indexIDs = strings.Replace(fmt.Sprintf("%v", a.Ctx.GetSessionVars().StmtCtx.IndexIDs), " ", ",", -1)
}
execDetail := sessVars.StmtCtx.GetExecDetails()
statsInfos := a.getStatsInfo()
if costTime < threshold {
_, digest := sessVars.StmtCtx.SQLDigest()
logutil.SlowQueryLogger.Debug(sessVars.SlowLogFormat(txnTS, costTime, execDetail, indexIDs, digest, sql))
logutil.SlowQueryLogger.Debug(sessVars.SlowLogFormat(txnTS, costTime, execDetail, indexIDs, digest, statsInfos, sql))
} else {
_, digest := sessVars.StmtCtx.SQLDigest()
logutil.SlowQueryLogger.Warn(sessVars.SlowLogFormat(txnTS, costTime, execDetail, indexIDs, digest, sql))
logutil.SlowQueryLogger.Warn(sessVars.SlowLogFormat(txnTS, costTime, execDetail, indexIDs, digest, statsInfos, sql))
metrics.TotalQueryProcHistogram.Observe(costTime.Seconds())
metrics.TotalCopProcHistogram.Observe(execDetail.ProcessTime.Seconds())
metrics.TotalCopWaitHistogram.Observe(execDetail.WaitTime.Seconds())
Expand All @@ -444,6 +445,28 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
}
}

func (a *ExecStmt) getStatsInfo() map[string]uint64 {
var physicalPlan plannercore.PhysicalPlan
switch p := a.Plan.(type) {
case *plannercore.Insert:
physicalPlan = p.SelectPlan
case *plannercore.Update:
physicalPlan = p.SelectPlan
case *plannercore.Delete:
physicalPlan = p.SelectPlan
case plannercore.PhysicalPlan:
physicalPlan = p
}

if physicalPlan == nil {
return nil
}

statsInfos := make(map[string]uint64)
statsInfos = plannercore.CollectPlanStatsVersion(physicalPlan, statsInfos)
return statsInfos
}

// IsPointGetWithPKOrUniqueKeyByAutoCommit returns true when meets following conditions:
// 1. ctx is auto commit tagged
// 2. txn is not valid
Expand Down
2 changes: 1 addition & 1 deletion executor/index_lookup_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s *testSuite1) TestIndexJoinUnionScan(c *C) {
" └─IndexLookUp_11 0.00 root ",
" ├─Selection_10 0.00 cop not(isnull(test.t2.a))",
" │ └─IndexScan_8 10.00 cop table:t2, index:a, range: decided by [test.t1.a], keep order:false, stats:pseudo",
" └─TableScan_9 0.00 cop table:t2, keep order:false",
" └─TableScan_9 0.00 cop table:t2, keep order:false, stats:pseudo",
))
tk.MustQuery("select /*+ TIDB_INLJ(t1, t2)*/ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows(
"2 2 2 2 2",
Expand Down
5 changes: 5 additions & 0 deletions infoschema/slow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var slowQueryCols = []columnInfo{
{variable.SlowLogIndexIDsStr, mysql.TypeVarchar, 100, 0, nil, nil},
{variable.SlowLogIsInternalStr, mysql.TypeTiny, 1, 0, nil, nil},
{variable.SlowLogDigestStr, mysql.TypeVarchar, 64, 0, nil, nil},
{variable.SlowLogStatsInfoStr, mysql.TypeVarchar, 512, 0, nil, nil},
{variable.SlowLogQuerySQLStr, mysql.TypeVarchar, 4096, 0, nil, nil},
}

Expand Down Expand Up @@ -139,6 +140,7 @@ type slowQueryTuple struct {
indexIDs string
isInternal bool
digest string
statsInfo string
sql string
}

Expand Down Expand Up @@ -217,6 +219,8 @@ func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string)
st.isInternal = value == "true"
case variable.SlowLogDigestStr:
st.digest = value
case variable.SlowLogStatsInfoStr:
st.statsInfo = value
case variable.SlowLogQuerySQLStr:
st.sql = value
}
Expand Down Expand Up @@ -244,6 +248,7 @@ func (st *slowQueryTuple) convertToDatumRow() []types.Datum {
record = append(record, types.NewStringDatum(st.indexIDs))
record = append(record, types.NewDatum(st.isInternal))
record = append(record, types.NewStringDatum(st.digest))
record = append(record, types.NewStringDatum(st.statsInfo))
record = append(record, types.NewStringDatum(st.sql))
return record
}
Expand Down
3 changes: 2 additions & 1 deletion infoschema/slow_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (s *testSuite) TestParseSlowLogFile(c *C) {
# Process_time: 0.021 Request_count: 1 Total_keys: 637 Processed_keys: 436
# Is_internal: true
# Digest: 42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772
# Stats: t1:1,t2:2
select * from t;`)
scanner := bufio.NewScanner(slowLog)
loc, err := time.LoadLocation("Asia/Shanghai")
Expand All @@ -47,7 +48,7 @@ select * from t;`)
}
recordString += str
}
expectRecordString := "2019-01-24 22:32:29.313255,405888132465033227,,0,0.216905,0.021,0,0,1,637,0,,,1,42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772,select * from t;"
expectRecordString := "2019-01-24 22:32:29.313255,405888132465033227,,0,0.216905,0.021,0,0,1,637,0,,,1,42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772,t1:1,t2:2,select * from t;"
c.Assert(expectRecordString, Equals, recordString)
}

Expand Down
5 changes: 3 additions & 2 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,16 @@ func (s *testTableSuite) TestSlowQuery(c *C) {
# DB: test
# Is_internal: false
# Digest: 42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772
# Stats: t1:1,t2:2
select * from t_slim;`))
c.Assert(f.Close(), IsNil)
c.Assert(err, IsNil)

tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", slowLogFileName))
tk.MustExec("set time_zone = '+08:00';")
re := tk.MustQuery("select * from information_schema.slow_query")
re.Check(testutil.RowsWithSep("|", "2019-02-12 19:33:56.571953|406315658548871171|[email protected]|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|select * from t_slim;"))
re.Check(testutil.RowsWithSep("|", "2019-02-12 19:33:56.571953|406315658548871171|[email protected]|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|t1:1,t2:2|select * from t_slim;"))
tk.MustExec("set time_zone = '+00:00';")
re = tk.MustQuery("select * from information_schema.slow_query")
re.Check(testutil.RowsWithSep("|", "2019-02-12 11:33:56.571953|406315658548871171|[email protected]|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|select * from t_slim;"))
re.Check(testutil.RowsWithSep("|", "2019-02-12 11:33:56.571953|406315658548871171|[email protected]|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|t1:1,t2:2|select * from t_slim;"))
}
11 changes: 9 additions & 2 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/expression/aggregation"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/ranger"
"github.com/pingcap/tidb/util/set"
Expand Down Expand Up @@ -462,7 +463,10 @@ func (p *LogicalJoin) constructInnerTableScan(ds *DataSource, pk *expression.Col
ts.SetSchema(ds.schema)

ts.stats = property.NewSimpleStats(1)
ts.stats.UsePseudoStats = ds.statisticTable.Pseudo
ts.stats.StatsVersion = ds.statisticTable.Version
if ds.statisticTable.Pseudo {
ts.stats.StatsVersion = statistics.PseudoVersion
}

copTask := &copTask{
tablePlan: ts,
Expand Down Expand Up @@ -509,7 +513,10 @@ func (p *LogicalJoin) constructInnerIndexScan(ds *DataSource, idx *model.IndexIn
rowCount = ds.statisticTable.PseudoAvgCountPerValue()
}
is.stats = property.NewSimpleStats(rowCount)
is.stats.UsePseudoStats = ds.statisticTable.Pseudo
is.stats.StatsVersion = ds.statisticTable.Version
if ds.statisticTable.Pseudo {
is.stats.StatsVersion = statistics.PseudoVersion
}

cop := &copTask{
indexPlan: is,
Expand Down
5 changes: 3 additions & 2 deletions planner/core/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/expression/aggregation"
"github.com/pingcap/tidb/statistics"
)

// ExplainInfo implements PhysicalPlan interface.
Expand Down Expand Up @@ -74,7 +75,7 @@ func (p *PhysicalIndexScan) ExplainInfo() string {
if p.Desc {
buffer.WriteString(", desc")
}
if p.stats.UsePseudoStats {
if p.stats.StatsVersion == statistics.PseudoVersion {
buffer.WriteString(", stats:pseudo")
}
return buffer.String()
Expand Down Expand Up @@ -121,7 +122,7 @@ func (p *PhysicalTableScan) ExplainInfo() string {
if p.Desc {
buffer.WriteString(", desc")
}
if p.stats.UsePseudoStats {
if p.stats.StatsVersion == statistics.PseudoVersion {
buffer.WriteString(", stats:pseudo")
}
return buffer.String()
Expand Down
13 changes: 11 additions & 2 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"golang.org/x/tools/container/intsets"
Expand Down Expand Up @@ -505,7 +506,11 @@ func (ds *DataSource) convertToIndexScan(prop *property.PhysicalProperty, candid
rowCount = math.Min(prop.ExpectedCnt/selectivity, rowCount)
}
is.stats = property.NewSimpleStats(rowCount)
is.stats.UsePseudoStats = ds.statisticTable.Pseudo
is.stats.StatsVersion = ds.statisticTable.Version
if ds.statisticTable.Pseudo {
is.stats.StatsVersion = statistics.PseudoVersion
}

cop.cst = rowCount * scanFactor
task = cop
if candidate.isMatchProp {
Expand Down Expand Up @@ -654,7 +659,11 @@ func (ds *DataSource) convertToTableScan(prop *property.PhysicalProperty, candid
rowCount = math.Min(prop.ExpectedCnt/selectivity, rowCount)
}
ts.stats = property.NewSimpleStats(rowCount)
ts.stats.UsePseudoStats = ds.statisticTable.Pseudo
ts.stats.StatsVersion = ds.statisticTable.Version
if ds.statisticTable.Pseudo {
ts.stats.StatsVersion = statistics.PseudoVersion
}

copTask.cst = rowCount * scanFactor
if candidate.isMatchProp {
if prop.Items[0].Desc {
Expand Down
23 changes: 23 additions & 0 deletions planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,26 @@ type PhysicalWindow struct {
OrderBy []property.Item
Frame *WindowFrame
}

// CollectPlanStatsVersion uses to collect the statistics version of the plan.
func CollectPlanStatsVersion(plan PhysicalPlan, statsInfos map[string]uint64) map[string]uint64 {
for _, child := range plan.Children() {
statsInfos = CollectPlanStatsVersion(child, statsInfos)
}
switch copPlan := plan.(type) {
case *PhysicalTableReader:
statsInfos = CollectPlanStatsVersion(copPlan.tablePlan, statsInfos)
case *PhysicalIndexReader:
statsInfos = CollectPlanStatsVersion(copPlan.indexPlan, statsInfos)
case *PhysicalIndexLookUpReader:
// For index loop up, only the indexPlan is necessary,
// because they use the same stats and we do not set the stats info for tablePlan.
statsInfos = CollectPlanStatsVersion(copPlan.indexPlan, statsInfos)
case *PhysicalIndexScan:
statsInfos[copPlan.Table.Name.O] = copPlan.stats.StatsVersion
case *PhysicalTableScan:
statsInfos[copPlan.Table.Name.O] = copPlan.stats.StatsVersion
}

return statsInfos
}
12 changes: 8 additions & 4 deletions planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ func (ds *DataSource) getColumnNDV(colID int64) (ndv float64) {

func (ds *DataSource) getStatsByFilter(conds expression.CNFExprs) (*property.StatsInfo, *statistics.HistColl) {
profile := &property.StatsInfo{
RowCount: float64(ds.statisticTable.Count),
Cardinality: make([]float64, len(ds.Columns)),
HistColl: ds.statisticTable.GenerateHistCollFromColumnInfo(ds.Columns, ds.schema.Columns),
UsePseudoStats: ds.statisticTable.Pseudo,
RowCount: float64(ds.statisticTable.Count),
Cardinality: make([]float64, len(ds.Columns)),
HistColl: ds.statisticTable.GenerateHistCollFromColumnInfo(ds.Columns, ds.schema.Columns),
StatsVersion: ds.statisticTable.Version,
}
if ds.statisticTable.Pseudo {
profile.StatsVersion = statistics.PseudoVersion
}

for i, col := range ds.Columns {
profile.Cardinality[i] = ds.getColumnNDV(col.ID)
}
Expand Down
14 changes: 7 additions & 7 deletions planner/property/stats_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type StatsInfo struct {
Cardinality []float64

HistColl *statistics.HistColl
// UsePseudoStats indicates whether the StatsInfo is calculated using the
// pseudo statistics on a table.
UsePseudoStats bool
// StatsVersion indicates the statistics version of a table.
// If the StatsInfo is calculated using the pseudo statistics on a table, StatsVersion will be PseudoVersion.
StatsVersion uint64
}

// NewSimpleStats creates a simple StatsInfo with rowCount.
Expand All @@ -48,10 +48,10 @@ func (s *StatsInfo) Count() int64 {
// Scale receives a selectivity and multiplies it with RowCount and Cardinality.
func (s *StatsInfo) Scale(factor float64) *StatsInfo {
profile := &StatsInfo{
RowCount: s.RowCount * factor,
Cardinality: make([]float64, len(s.Cardinality)),
HistColl: s.HistColl,
UsePseudoStats: s.UsePseudoStats,
RowCount: s.RowCount * factor,
Cardinality: make([]float64, len(s.Cardinality)),
HistColl: s.HistColl,
StatsVersion: s.StatsVersion,
}
for i := range profile.Cardinality {
profile.Cardinality[i] = s.Cardinality[i] * factor
Expand Down
Loading