Skip to content

Commit

Permalink
stmtsummary: stmtsummary with keyspace (#41882)
Browse files Browse the repository at this point in the history
close #41881
  • Loading branch information
zeminzhou authored Mar 6, 2023
1 parent 7be60d9 commit f43b1bb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ go_library(
"//expression",
"//expression/aggregation",
"//infoschema",
"//keyspace",
"//kv",
"//meta",
"//meta/autoid",
Expand Down
12 changes: 12 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
executor_metrics "github.com/pingcap/tidb/executor/metrics"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/keyspace"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser"
Expand Down Expand Up @@ -1821,6 +1822,15 @@ func (a *ExecStmt) SummaryStmt(succ bool) {

resultRows := GetResultRowsCount(stmtCtx, a.Plan)

var (
keyspaceName string
keyspaceID uint32
)
keyspaceName = keyspace.GetKeyspaceNameBySettings()
if !keyspace.IsKeyspaceNameEmpty(keyspaceName) {
keyspaceID = uint32(a.Ctx.GetStore().GetCodec().GetKeyspaceID())
}

stmtExecInfo := &stmtsummary.StmtExecInfo{
SchemaName: strings.ToLower(sessVars.CurrentDB),
OriginalSQL: sql,
Expand Down Expand Up @@ -1853,6 +1863,8 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
ResultRows: resultRows,
TiKVExecDetails: tikvExecDetail,
Prepared: a.isPreparedStmt,
KeyspaceName: keyspaceName,
KeyspaceID: keyspaceID,
}
if a.retryCount > 0 {
stmtExecInfo.ExecRetryTime = costTime - sessVars.DurationParse - sessVars.DurationCompile - time.Since(a.retryStartTime)
Expand Down
2 changes: 2 additions & 0 deletions util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ type StmtExecInfo struct {
ResultRows int64
TiKVExecDetails util.ExecDetails
Prepared bool
KeyspaceName string
KeyspaceID uint32
}

// newStmtSummaryByDigestMap creates an empty stmtSummaryByDigestMap.
Expand Down
15 changes: 11 additions & 4 deletions util/stmtsummary/v2/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ type StmtRecord struct {
// Pessimistic execution retry information.
ExecRetryCount uint `json:"exec_retry_count"`
ExecRetryTime time.Duration `json:"exec_retry_time"`

KeyspaceName string `json:"keyspace_name,omitempty"`
KeyspaceID uint32 `json:"keyspace_id,omitempty"`
}

// NewStmtRecord creates a new StmtRecord from StmtExecInfo.
Expand Down Expand Up @@ -209,6 +212,8 @@ func NewStmtRecord(info *stmtsummary.StmtExecInfo) *StmtRecord {
Prepared: info.Prepared,
FirstSeen: info.StartTime,
LastSeen: info.StartTime,
KeyspaceName: info.KeyspaceName,
KeyspaceID: info.KeyspaceID,
}
}

Expand Down Expand Up @@ -651,10 +656,12 @@ func GenerateStmtExecInfo4Test(digest string) *stmtsummary.StmtExecInfo {
Tables: tables,
IndexNames: indexes,
},
MemMax: 10000,
DiskMax: 10000,
StartTime: time.Date(2019, 1, 1, 10, 10, 10, 10, time.UTC),
Succeed: true,
MemMax: 10000,
DiskMax: 10000,
StartTime: time.Date(2019, 1, 1, 10, 10, 10, 10, time.UTC),
Succeed: true,
KeyspaceName: "keyspace_a",
KeyspaceID: 1,
}
stmtExecInfo.StmtCtx.AddAffectedRows(10000)
return stmtExecInfo
Expand Down
2 changes: 2 additions & 0 deletions util/stmtsummary/v2/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestStmtRecord(t *testing.T) {
require.Equal(t, info.Prepared, record1.Prepared)
require.Equal(t, info.StartTime, record1.FirstSeen)
require.Equal(t, info.StartTime, record1.LastSeen)
require.Equal(t, info.KeyspaceName, record1.KeyspaceName)
require.Equal(t, info.KeyspaceID, record1.KeyspaceID)
require.Empty(t, record1.AuthUsers)
require.Zero(t, record1.ExecCount)
require.Zero(t, record1.SumLatency)
Expand Down

0 comments on commit f43b1bb

Please sign in to comment.