Skip to content

Commit

Permalink
stats: reduce conflict for updating column size (#7124) (#7138)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and coocood committed Jul 24, 2018
1 parent 0338239 commit c028f5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
19 changes: 11 additions & 8 deletions statistics/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"math"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -221,16 +222,18 @@ func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) e
return errors.Trace(err)
}
version := h.ctx.Txn().StartTS()

for key, val := range delta.ColSize {
if val == 0 {
values := make([]string, 0, len(delta.ColSize))
for histID, deltaColSize := range delta.ColSize {
if deltaColSize == 0 {
continue
}
sql := fmt.Sprintf("update mysql.stats_histograms set version = %d, tot_col_size = tot_col_size + %d where hist_id = %d and table_id = %d and is_index = 0", version, val, key, id)
_, err = h.ctx.(sqlexec.SQLExecutor).Execute(ctx, sql)
if err != nil {
return errors.Trace(err)
}
values = append(values, fmt.Sprintf("(%d, 0, %d, 0, %d, %d)", id, histID, deltaColSize, version))
}
sql := fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, tot_col_size, version) "+
"values %s on duplicate key update tot_col_size = tot_col_size + values(tot_col_size), version = values(version)", strings.Join(values, ","))
_, err = h.ctx.(sqlexec.SQLExecutor).Execute(ctx, sql)
if err != nil {
return errors.Trace(err)
}
_, err = h.ctx.(sqlexec.SQLExecutor).Execute(ctx, "commit")
return errors.Trace(err)
Expand Down
3 changes: 2 additions & 1 deletion statistics/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ func (s *testStatsUpdateSuite) TestSingleSessionInsert(c *C) {
rs := testKit.MustQuery("select modify_count from mysql.stats_meta")
rs.Check(testkit.Rows("40", "70"))

rs = testKit.MustQuery("select tot_col_size from mysql.stats_histograms")
rs = testKit.MustQuery(fmt.Sprintf("select tot_col_size from mysql.stats_histograms where table_id in (%d, %d)"+
" order by tot_col_size", tbl2.Meta().ID, tbl1.Meta().ID))
rs.Check(testkit.Rows("0", "0", "10", "10"))
}

Expand Down

0 comments on commit c028f5f

Please sign in to comment.