Skip to content

Commit

Permalink
infoschema, util: Add more fields to table `events_statements_summary…
Browse files Browse the repository at this point in the history
…_by_digest` (#13484) (#14151)
  • Loading branch information
djshow832 authored and zimulala committed Dec 20, 2019
1 parent b52cccd commit 620a6c0
Show file tree
Hide file tree
Showing 5 changed files with 1,036 additions and 287 deletions.
30 changes: 22 additions & 8 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,15 +775,29 @@ func (a *ExecStmt) SummaryStmt() {
stmtCtx := sessVars.StmtCtx
normalizedSQL, digest := stmtCtx.SQLDigest()
costTime := time.Since(sessVars.StartTime)

execDetail := stmtCtx.GetExecDetails()
copTaskInfo := stmtCtx.CopTasksDetails()
memMax := stmtCtx.MemTracker.MaxConsumed()
var userString string
if sessVars.User != nil {
userString = sessVars.User.String()
}

stmtsummary.StmtSummaryByDigestMap.AddStatement(&stmtsummary.StmtExecInfo{
SchemaName: sessVars.CurrentDB,
OriginalSQL: a.Text,
NormalizedSQL: normalizedSQL,
Digest: digest,
TotalLatency: uint64(costTime.Nanoseconds()),
AffectedRows: stmtCtx.AffectedRows(),
SentRows: 0,
StartTime: sessVars.StartTime,
SchemaName: strings.ToLower(sessVars.CurrentDB),
OriginalSQL: a.Text,
NormalizedSQL: normalizedSQL,
Digest: digest,
User: userString,
TotalLatency: costTime,
ParseLatency: sessVars.DurationParse,
CompileLatency: sessVars.DurationCompile,
StmtCtx: stmtCtx,
CopTasks: copTaskInfo,
ExecDetail: &execDetail,
MemMax: memMax,
StartTime: sessVars.StartTime,
})
}

Expand Down
54 changes: 51 additions & 3 deletions infoschema/perfschema/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,63 @@ const tableStagesHistoryLong = "CREATE TABLE if not exists performance_schema.ev
// tableEventsStatementsSummaryByDigest contains the column name definitions for table
// events_statements_summary_by_digest, same as MySQL.
const tableEventsStatementsSummaryByDigest = "CREATE TABLE if not exists events_statements_summary_by_digest (" +
"STMT_TYPE VARCHAR(64) NOT NULL," +
"SCHEMA_NAME VARCHAR(64) DEFAULT NULL," +
"DIGEST VARCHAR(64) DEFAULT NULL," +
"DIGEST_TEXT LONGTEXT DEFAULT NULL," +
"DIGEST VARCHAR(64) NOT NULL," +
"DIGEST_TEXT LONGTEXT NOT NULL," +
"TABLE_NAMES TEXT DEFAULT NULL," +
"INDEX_NAMES TEXT DEFAULT NULL," +
"SAMPLE_USER VARCHAR(64) DEFAULT NULL," +
"EXEC_COUNT BIGINT(20) UNSIGNED NOT NULL," +
"SUM_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"MAX_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"MIN_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"AVG_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"SUM_ROWS_AFFECTED BIGINT(20) UNSIGNED NOT NULL," +
"AVG_PARSE_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"MAX_PARSE_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"AVG_COMPILE_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"MAX_COMPILE_LATENCY BIGINT(20) UNSIGNED NOT NULL," +
"COP_TASK_NUM BIGINT(20) UNSIGNED NOT NULL," +
"AVG_COP_PROCESS_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_COP_PROCESS_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_COP_PROCESS_ADDRESS VARCHAR(256) DEFAULT NULL," +
"AVG_COP_WAIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_COP_WAIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_COP_WAIT_ADDRESS VARCHAR(256) DEFAULT NULL," +
"AVG_PROCESS_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_PROCESS_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_WAIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_WAIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_BACKOFF_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_BACKOFF_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_TOTAL_KEYS BIGINT(20) UNSIGNED NOT NULL," +
"MAX_TOTAL_KEYS BIGINT(20) UNSIGNED NOT NULL," +
"AVG_PROCESSED_KEYS BIGINT(20) UNSIGNED NOT NULL," +
"MAX_PROCESSED_KEYS BIGINT(20) UNSIGNED NOT NULL," +
"AVG_PREWRITE_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_PREWRITE_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_COMMIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_COMMIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_GET_COMMIT_TS_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_GET_COMMIT_TS_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_COMMIT_BACKOFF_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_COMMIT_BACKOFF_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_RESOLVE_LOCK_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_RESOLVE_LOCK_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_LOCAL_LATCH_WAIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"MAX_LOCAL_LATCH_WAIT_TIME BIGINT(20) UNSIGNED NOT NULL," +
"AVG_WRITE_KEYS DOUBLE UNSIGNED NOT NULL," +
"MAX_WRITE_KEYS BIGINT(20) UNSIGNED NOT NULL," +
"AVG_WRITE_SIZE DOUBLE NOT NULL," +
"MAX_WRITE_SIZE BIGINT(20) UNSIGNED NOT NULL," +
"AVG_PREWRITE_REGIONS DOUBLE NOT NULL," +
"MAX_PREWRITE_REGIONS INT(11) UNSIGNED NOT NULL," +
"AVG_TXN_RETRY DOUBLE NOT NULL," +
"MAX_TXN_RETRY INT(11) UNSIGNED NOT NULL," +
"BACKOFF_TYPES VARCHAR(1024) DEFAULT NULL," +
"AVG_MEM BIGINT(20) UNSIGNED NOT NULL," +
"MAX_MEM BIGINT(20) UNSIGNED NOT NULL," +
"AVG_AFFECTED_ROWS DOUBLE UNSIGNED NOT NULL," +
"FIRST_SEEN TIMESTAMP(6) NOT NULL," +
"LAST_SEEN TIMESTAMP(6) NOT NULL," +
"QUERY_SAMPLE_TEXT LONGTEXT DEFAULT NULL);"
Expand Down
56 changes: 43 additions & 13 deletions infoschema/perfschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b varchar(10))")
tk.MustExec("create table t(a int, b varchar(10), key k(a))")

// Statement summary is disabled by default
tk.MustQuery("select @@global.tidb_enable_stmt_summary").Check(testkit.Rows("0"))
Expand All @@ -90,23 +90,29 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk.MustExec("insert into t values(2, 'b')")
tk.MustExec("insert into t VALUES(3, 'c')")
tk.MustExec("/**/insert into t values(4, 'd')")
tk.MustQuery(`select schema_name, exec_count, sum_rows_affected, query_sample_text
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest
where digest_text like 'insert into t%'`,
).Check(testkit.Rows("test 4 4 insert into t values(1, 'a')"))
).Check(testkit.Rows("insert test test.t <nil> 4 0 0 0 0 0 2 2 1 1 1 /**/insert into t values(4, 'd')"))

// Test SELECT
tk.MustQuery("select * from t where a=2")
tk.MustQuery(`select schema_name, exec_count, sum_rows_affected, query_sample_text
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest
where digest_text like 'select * from t%'`,
).Check(testkit.Rows("test 1 0 select * from t where a=2"))
).Check(testkit.Rows("select test test.t t:k 1 2 0 0 0 0 0 0 0 0 0 select * from t where a=2"))

// select ... order by
tk.MustQuery(`select schema_name, exec_count, sum_rows_affected, query_sample_text
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest
order by exec_count desc limit 1`,
).Check(testkit.Rows("test 4 4 insert into t values(1, 'a')"))
).Check(testkit.Rows("insert test test.t <nil> 4 0 0 0 0 0 2 2 1 1 1 /**/insert into t values(4, 'd')"))

// Disable it again
tk.MustExec("set global tidb_enable_stmt_summary = false")
Expand All @@ -119,18 +125,38 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk.MustQuery("select * from t where a=2")

// The table should be cleared
tk.MustQuery(`select schema_name, exec_count, sum_rows_affected, query_sample_text
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest`,
).Check(testkit.Rows())

// Enable it in session scope
tk.MustExec("set session tidb_enable_stmt_summary = on")
// It should work immediately
tk.MustExec("begin")
tk.MustExec("insert into t values(1, 'a')")
tk.MustExec("commit")
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest
where digest_text like 'insert into t%'`,
).Check(testkit.Rows("insert test test.t <nil> 1 0 0 0 0 0 0 0 0 0 1 insert into t values(1, 'a')"))
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest
where digest_text='commit'`,
).Check(testkit.Rows("commit test <nil> <nil> 1 0 0 0 0 0 2 2 1 1 0 commit"))

tk.MustQuery("select * from t where a=2")
tk.MustQuery(`select schema_name, exec_count, sum_rows_affected, query_sample_text
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest
where digest_text like 'select * from t%'`,
).Check(testkit.Rows("test 1 0 select * from t where a=2"))
).Check(testkit.Rows("select test test.t t:k 1 2 0 0 0 0 0 0 0 0 0 select * from t where a=2"))

// Disable it in global scope
tk.MustExec("set global tidb_enable_stmt_summary = off")
Expand All @@ -141,17 +167,21 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk.MustQuery("select * from t where a=2")

// Statement summary is still enabled
tk.MustQuery(`select schema_name, exec_count, sum_rows_affected, query_sample_text
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest
where digest_text like 'select * from t%'`,
).Check(testkit.Rows("test 2 0 select * from t where a=2"))
).Check(testkit.Rows("select test test.t t:k 2 4 0 0 0 0 0 0 0 0 0 select * from t where a=2"))

// Unset session variable
tk.MustExec("set session tidb_enable_stmt_summary = ''")
tk.MustQuery("select * from t where a=2")

// Statement summary is disabled
tk.MustQuery(`select schema_name, exec_count, sum_rows_affected, query_sample_text
tk.MustQuery(`select stmt_type, schema_name, table_names, index_names, exec_count, cop_task_num, avg_total_keys,
max_total_keys, avg_processed_keys, max_processed_keys, avg_write_keys, max_write_keys, avg_prewrite_regions,
max_prewrite_regions, avg_affected_rows, query_sample_text
from performance_schema.events_statements_summary_by_digest`,
).Check(testkit.Rows())
}
Loading

0 comments on commit 620a6c0

Please sign in to comment.