diff --git a/executor/adapter.go b/executor/adapter.go index 33422ab0bed9c..5397451f1bb5d 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -824,6 +824,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) { copTaskInfo := sessVars.StmtCtx.CopTasksDetails() statsInfos := plannercore.GetStatsInfo(a.Plan) memMax := sessVars.StmtCtx.MemTracker.MaxConsumed() + diskMax := sessVars.StmtCtx.DiskTracker.MaxConsumed() _, digest := sessVars.StmtCtx.SQLDigest() _, planDigest := getPlanDigest(a.Ctx, a.Plan) slowItems := &variable.SlowQueryLogItems{ @@ -838,6 +839,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) { CopTasks: copTaskInfo, ExecDetail: execDetail, MemMax: memMax, + DiskMax: diskMax, Succ: succ, Plan: getPlanTree(a.Plan), PlanDigest: planDigest, @@ -944,6 +946,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) { execDetail := stmtCtx.GetExecDetails() copTaskInfo := stmtCtx.CopTasksDetails() memMax := stmtCtx.MemTracker.MaxConsumed() + diskMax := stmtCtx.DiskTracker.MaxConsumed() var userString string if sessVars.User != nil { userString = sessVars.User.Username @@ -967,6 +970,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) { CopTasks: copTaskInfo, ExecDetail: &execDetail, MemMax: memMax, + DiskMax: diskMax, StartTime: sessVars.StartTime, IsInternal: sessVars.InRestrictedSQL, Succeed: succ, diff --git a/executor/slow_query.go b/executor/slow_query.go index 30ea85a432033..ab628ae2d1af0 100644 --- a/executor/slow_query.go +++ b/executor/slow_query.go @@ -339,6 +339,7 @@ type slowQueryTuple struct { maxWaitTime float64 maxWaitAddress string memMax int64 + diskMax int64 prevStmt string sql string isInternal bool @@ -452,6 +453,8 @@ func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string, st.maxWaitAddress = value case variable.SlowLogMemMax: st.memMax, err = strconv.ParseInt(value, 10, 64) + case variable.SlowLogDiskMax: + st.diskMax, err = strconv.ParseInt(value, 10, 64) case variable.SlowLogSucc: st.succ, err = strconv.ParseBool(value) case variable.SlowLogPlan: @@ -511,6 +514,7 @@ func (st *slowQueryTuple) convertToDatumRow() []types.Datum { record = append(record, types.NewFloat64Datum(st.maxWaitTime)) record = append(record, types.NewStringDatum(st.maxWaitAddress)) record = append(record, types.NewIntDatum(st.memMax)) + record = append(record, types.NewIntDatum(st.diskMax)) if st.succ { record = append(record, types.NewIntDatum(1)) } else { diff --git a/infoschema/tables.go b/infoschema/tables.go index 2419feb8a6937..872758f2701a7 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -745,6 +745,7 @@ var slowQueryCols = []columnInfo{ {name: variable.SlowLogCopWaitMax, tp: mysql.TypeDouble, size: 22}, {name: variable.SlowLogCopWaitAddr, tp: mysql.TypeVarchar, size: 64}, {name: variable.SlowLogMemMax, tp: mysql.TypeLonglong, size: 20}, + {name: variable.SlowLogDiskMax, tp: mysql.TypeLonglong, size: 20}, {name: variable.SlowLogSucc, tp: mysql.TypeTiny, size: 1}, {name: variable.SlowLogPlan, tp: mysql.TypeLongBlob, size: types.UnspecifiedLength}, {name: variable.SlowLogPlanDigest, tp: mysql.TypeVarchar, size: 128}, @@ -1094,6 +1095,8 @@ var tableStatementsSummaryCols = []columnInfo{ {name: "BACKOFF_TYPES", tp: mysql.TypeVarchar, size: 1024, comment: "Types of errors and the number of retries for each type"}, {name: "AVG_MEM", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Average memory(byte) used"}, {name: "MAX_MEM", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Max memory(byte) used"}, + {name: "AVG_DISK", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Average disk(byte) used"}, + {name: "MAX_DISK", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Max disk(byte) used"}, {name: "AVG_AFFECTED_ROWS", tp: mysql.TypeDouble, size: 22, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Average number of rows affected"}, {name: "FIRST_SEEN", tp: mysql.TypeTimestamp, size: 26, flag: mysql.NotNullFlag, comment: "The time these statements are seen for the first time"}, {name: "LAST_SEEN", tp: mysql.TypeTimestamp, size: 26, flag: mysql.NotNullFlag, comment: "The time these statements are seen for the last time"}, diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index 32a7011735217..7847c05ffcfce 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -1479,6 +1479,8 @@ const ( SlowLogCopBackoffPrefix = "Cop_backoff_" // SlowLogMemMax is the max number bytes of memory used in this statement. SlowLogMemMax = "Mem_max" + // SlowLogDiskMax is the max number bytes of disk used in this statement. + SlowLogDiskMax = "Disk_max" // SlowLogPrepared is used to indicate whether this sql execute in prepare. SlowLogPrepared = "Prepared" // SlowLogHasMoreResults is used to indicate whether this sql has more following results. @@ -1513,6 +1515,7 @@ type SlowQueryLogItems struct { CopTasks *stmtctx.CopTasksDetails ExecDetail execdetails.ExecDetails MemMax int64 + DiskMax int64 Succ bool Prepared bool HasMoreResults bool @@ -1642,6 +1645,9 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string { if logItems.MemMax > 0 { writeSlowLogItem(&buf, SlowLogMemMax, strconv.FormatInt(logItems.MemMax, 10)) } + if logItems.DiskMax > 0 { + writeSlowLogItem(&buf, SlowLogDiskMax, strconv.FormatInt(logItems.DiskMax, 10)) + } writeSlowLogItem(&buf, SlowLogPrepared, strconv.FormatBool(logItems.Prepared)) writeSlowLogItem(&buf, SlowLogHasMoreResults, strconv.FormatBool(logItems.HasMoreResults)) diff --git a/sessionctx/variable/session_test.go b/sessionctx/variable/session_test.go index 8e5136bebf995..6c481c015cda7 100644 --- a/sessionctx/variable/session_test.go +++ b/sessionctx/variable/session_test.go @@ -170,7 +170,10 @@ func (*testSessionSuite) TestSlowLogFormat(c *C) { copTasks.TotBackoffTimes[backoff] = 200 } - var memMax int64 = 2333 + var ( + memMax int64 = 2333 + diskMax int64 = 2333 + ) resultString := `# Txn_start_ts: 406649736972468225 # User: root@192.168.0.1 # Conn_ID: 1 @@ -190,6 +193,7 @@ func (*testSessionSuite) TestSlowLogFormat(c *C) { # Cop_backoff_rpcPD_total_times: 200 Cop_backoff_rpcPD_total_time: 0.2 Cop_backoff_rpcPD_max_time: 0.2 Cop_backoff_rpcPD_max_addr: 127.0.0.1 Cop_backoff_rpcPD_avg_time: 0.2 Cop_backoff_rpcPD_p90_time: 0.2 # Cop_backoff_rpcTiKV_total_times: 200 Cop_backoff_rpcTiKV_total_time: 0.2 Cop_backoff_rpcTiKV_max_time: 0.2 Cop_backoff_rpcTiKV_max_addr: 127.0.0.1 Cop_backoff_rpcTiKV_avg_time: 0.2 Cop_backoff_rpcTiKV_p90_time: 0.2 # Mem_max: 2333 +# Disk_max: 2333 # Prepared: true # Has_more_results: true # Succ: true @@ -208,6 +212,7 @@ select * from t;` CopTasks: copTasks, ExecDetail: execDetail, MemMax: memMax, + DiskMax: diskMax, Prepared: true, HasMoreResults: true, Succ: true, diff --git a/util/stmtsummary/statement_summary.go b/util/stmtsummary/statement_summary.go index 12558adaeb5b0..ffe7f50fd421e 100644 --- a/util/stmtsummary/statement_summary.go +++ b/util/stmtsummary/statement_summary.go @@ -163,6 +163,8 @@ type stmtSummaryByDigestElement struct { // other sumMem int64 maxMem int64 + sumDisk int64 + maxDisk int64 sumAffectedRows uint64 // The first time this type of SQL executes. firstSeen time.Time @@ -189,6 +191,7 @@ type StmtExecInfo struct { CopTasks *stmtctx.CopTasksDetails ExecDetail *execdetails.ExecDetails MemMax int64 + DiskMax int64 StartTime time.Time IsInternal bool Succeed bool @@ -727,6 +730,10 @@ func (ssElement *stmtSummaryByDigestElement) add(sei *StmtExecInfo, intervalSeco if sei.MemMax > ssElement.maxMem { ssElement.maxMem = sei.MemMax } + ssElement.sumDisk += sei.DiskMax + if sei.DiskMax > ssElement.maxDisk { + ssElement.maxDisk = sei.DiskMax + } if sei.StartTime.Before(ssElement.firstSeen) { ssElement.firstSeen = sei.StartTime } @@ -812,6 +819,8 @@ func (ssElement *stmtSummaryByDigestElement) toDatum(ssbd *stmtSummaryByDigest) formatBackoffTypes(ssElement.backoffTypes), avgInt(ssElement.sumMem, ssElement.execCount), ssElement.maxMem, + avgInt(ssElement.sumDisk, ssElement.execCount), + ssElement.maxDisk, avgFloat(int64(ssElement.sumAffectedRows), ssElement.execCount), types.NewTime(types.FromGoTime(ssElement.firstSeen), mysql.TypeTimestamp, 0), types.NewTime(types.FromGoTime(ssElement.lastSeen), mysql.TypeTimestamp, 0), diff --git a/util/stmtsummary/statement_summary_test.go b/util/stmtsummary/statement_summary_test.go index 1f08ecd9e16e6..eed0783a4579c 100644 --- a/util/stmtsummary/statement_summary_test.go +++ b/util/stmtsummary/statement_summary_test.go @@ -127,6 +127,8 @@ func (s *testStmtSummarySuite) TestAddStatement(c *C) { backoffTypes: make(map[fmt.Stringer]int), sumMem: stmtExecInfo1.MemMax, maxMem: stmtExecInfo1.MemMax, + sumDisk: stmtExecInfo1.DiskMax, + maxDisk: stmtExecInfo1.DiskMax, sumAffectedRows: stmtExecInfo1.StmtCtx.AffectedRows(), firstSeen: stmtExecInfo1.StartTime, lastSeen: stmtExecInfo1.StartTime, @@ -204,6 +206,7 @@ func (s *testStmtSummarySuite) TestAddStatement(c *C) { IndexNames: indexes, }, MemMax: 20000, + DiskMax: 20000, StartTime: time.Date(2019, 1, 1, 10, 10, 20, 10, time.UTC), Succeed: true, } @@ -254,6 +257,8 @@ func (s *testStmtSummarySuite) TestAddStatement(c *C) { expectedSummaryElement.backoffTypes[tikv.BoTxnLock] = 1 expectedSummaryElement.sumMem += stmtExecInfo2.MemMax expectedSummaryElement.maxMem = stmtExecInfo2.MemMax + expectedSummaryElement.sumDisk += stmtExecInfo2.DiskMax + expectedSummaryElement.maxDisk = stmtExecInfo2.DiskMax expectedSummaryElement.sumAffectedRows += stmtExecInfo2.StmtCtx.AffectedRows() expectedSummaryElement.lastSeen = stmtExecInfo2.StartTime @@ -469,6 +474,8 @@ func matchStmtSummaryByDigest(first, second *stmtSummaryByDigest) bool { ssElement1.sumBackoffTimes != ssElement2.sumBackoffTimes || ssElement1.sumMem != ssElement2.sumMem || ssElement1.maxMem != ssElement2.maxMem || + ssElement1.sumDisk != ssElement2.sumDisk || + ssElement1.maxDisk != ssElement2.maxDisk || ssElement1.sumAffectedRows != ssElement2.sumAffectedRows || ssElement1.firstSeen != ssElement2.firstSeen || ssElement1.lastSeen != ssElement2.lastSeen { @@ -564,6 +571,7 @@ func generateAnyExecInfo() *StmtExecInfo { IndexNames: indexes, }, MemMax: 10000, + DiskMax: 10000, StartTime: time.Date(2019, 1, 1, 10, 10, 10, 10, time.UTC), Succeed: true, } @@ -605,7 +613,7 @@ func (s *testStmtSummarySuite) TestToDatum(c *C) { stmtExecInfo1.ExecDetail.CommitDetail.WriteSize, stmtExecInfo1.ExecDetail.CommitDetail.WriteSize, stmtExecInfo1.ExecDetail.CommitDetail.PrewriteRegionNum, stmtExecInfo1.ExecDetail.CommitDetail.PrewriteRegionNum, stmtExecInfo1.ExecDetail.CommitDetail.TxnRetry, stmtExecInfo1.ExecDetail.CommitDetail.TxnRetry, 1, - "txnLock:1", stmtExecInfo1.MemMax, stmtExecInfo1.MemMax, stmtExecInfo1.StmtCtx.AffectedRows(), + "txnLock:1", stmtExecInfo1.MemMax, stmtExecInfo1.MemMax, stmtExecInfo1.DiskMax, stmtExecInfo1.DiskMax, stmtExecInfo1.StmtCtx.AffectedRows(), t, t, stmtExecInfo1.OriginalSQL, stmtExecInfo1.PrevSQL, "plan_digest", ""} match(c, datums[0], expectedDatum...)