Skip to content

Commit

Permalink
use UPDATE to dump data to KV
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelice committed Sep 30, 2020
1 parent 3507c00 commit 840b448
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ const (
);`

// CreateSchemaIndexUsageTable stores the index usage information.
CreateSchemaIndexUsageTable = `CREATE TABLE IF NOT EXISTS mysql.stats_index_usage (
CreateSchemaIndexUsageTable = `CREATE TABLE IF NOT EXISTS mysql.schema_index_usage (
TABLE_SCHEMA varchar(64),
TABLE_NAME varchar(64),
INDEX_NAME varchar(64),
QUERY_COUNT bigint(64),
ROWS_SELECTED bigint(64),
LAST_USED_AT timestamp,
INDEX(TABLE_SCHEMA, TABLE_NAME, INDEX_NAME)
PRIMARY KEY(TABLE_SCHEMA, TABLE_NAME, INDEX_NAME)
);`
)

Expand Down Expand Up @@ -422,7 +422,7 @@ const (
version48 = 48
// version49 introduces mysql.stats_extended table.
version49 = 49
// version50 add mysql.stats_index_usage table.
// version50 add mysql.schema_index_usage table.
version50 = 50
// version51 introduces CreateTablespacePriv to mysql.user.
version51 = 51
Expand Down Expand Up @@ -1247,7 +1247,7 @@ func doDDLWorks(s Session) {
mustExecute(s, CreateOptRuleBlacklist)
// Create stats_extended table.
mustExecute(s, CreateStatsExtended)
// Create stats_index_usage.
// Create schema_index_usage.
mustExecute(s, CreateSchemaIndexUsageTable)
}

Expand Down
12 changes: 6 additions & 6 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func cleanEnv(c *C, store kv.Storage, do *domain.Domain) {
tk.MustExec("delete from mysql.stats_histograms")
tk.MustExec("delete from mysql.stats_buckets")
tk.MustExec("delete from mysql.stats_extended")
tk.MustExec("delete from mysql.stats_index_usage")
tk.MustExec("delete from mysql.schema_index_usage")
do.StatsHandle().Clear()
}

Expand Down Expand Up @@ -769,27 +769,27 @@ func (s *testStatsSuite) TestIndexUsageInformation(c *C) {
tk.MustExec("create unique index idx_a on t_idx(a)")
tk.MustExec("create unique index idx_b on t_idx(b)")
tk.MustQuery("select a from t_idx where a=1")
querySQL := `select sum(query_count), sum(rows_selected) from mysql.stats_index_usage where table_name = "t_idx" group by table_schema, table_name, index_name`
querySQL := `select table_schema, table_name, index_name, query_count, rows_selected from mysql.schema_index_usage where table_name = "t_idx"`
do := s.do
err := do.StatsHandle().DumpIndexUsageToKV()
c.Assert(err, IsNil)
tk.MustQuery(querySQL).Check(testkit.Rows(
"1 0",
"test t_idx idx_a 1 0",
))
tk.MustExec("insert into t_idx values(1, 0)")
tk.MustQuery("select a from t_idx where a=1")
tk.MustQuery("select a from t_idx where a=1")
err = do.StatsHandle().DumpIndexUsageToKV()
c.Assert(err, IsNil)
tk.MustQuery(querySQL).Check(testkit.Rows(
"3 2",
"test t_idx idx_a 3 2",
))
tk.MustQuery("select b from t_idx where b=0")
tk.MustQuery("select b from t_idx where b=0")
err = do.StatsHandle().DumpIndexUsageToKV()
c.Assert(err, IsNil)
tk.MustQuery(querySQL).Check(testkit.Rows(
"3 2",
"2 2",
"test t_idx idx_a 3 2",
"test t_idx idx_b 2 2",
))
}
4 changes: 2 additions & 2 deletions statistics/handle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ func (h *Handle) DumpIndexUsageToKV() error {
for id, value := range mapper {
idInfo := strings.Split(id, ".")
sql := fmt.Sprintf(
`insert into mysql.STATS_INDEX_USAGE values ("%s", "%s", "%s", %d, %d, "%s") `,
idInfo[0], idInfo[1], idInfo[2], value.QueryCount, value.RowsSelected, value.LastUsedAt)
`insert into mysql.SCHEMA_INDEX_USAGE values ("%s", "%s", "%s", %d, %d, "%s") on duplicate key update query_count=query_count+%d, rows_selected=rows_selected+%d, last_used_at=greatest(last_used_at, "%s")`,
idInfo[0], idInfo[1], idInfo[2], value.QueryCount, value.RowsSelected, value.LastUsedAt, value.QueryCount, value.RowsSelected, value.LastUsedAt)
_, _, err := h.restrictedExec.ExecRestrictedSQL(sql)
if err != nil {
return err
Expand Down

0 comments on commit 840b448

Please sign in to comment.