Skip to content

Commit

Permalink
stats: update delta info for partition table (#7947)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and zz-jason committed Oct 18, 2018
1 parent b22bfb5 commit ec9672c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
8 changes: 0 additions & 8 deletions executor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,6 @@ func (e *DeleteExec) removeRow(ctx sessionctx.Context, t table.Table, h int64, d
return errors.Trace(err)
}
ctx.GetSessionVars().StmtCtx.AddAffectedRows(1)
colSize := make(map[int64]int64)
for id, col := range t.Cols() {
val := -int64(len(data[id].GetBytes()))
if val != 0 {
colSize[col.ID] = val
}
}
ctx.GetSessionVars().TxnCtx.UpdateDeltaForTable(t.Meta().ID, -1, 1, colSize)
return nil
}

Expand Down
9 changes: 0 additions & 9 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ func updateRecord(ctx sessionctx.Context, h int64, oldData, newData []types.Datu
}
}

// 6. Update delta for the statistics.
colSize := make(map[int64]int64)
for id, col := range t.Cols() {
val := int64(len(newData[id].GetBytes()) - len(oldData[id].GetBytes()))
if val != 0 {
colSize[col.ID] = val
}
}
ctx.GetSessionVars().TxnCtx.UpdateDeltaForTable(t.Meta().ID, 0, 1, colSize)
return true, handleChanged, newHandle, nil
}

Expand Down
51 changes: 51 additions & 0 deletions statistics/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,57 @@ func (s *testStatsUpdateSuite) TestTxnWithFailure(c *C) {
c.Assert(stats1.Count, Equals, int64(rowCount1+1))
}

func (s *testStatsUpdateSuite) TestUpdatePartition(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("set @@session.tidb_enable_table_partition=1")
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
createTable := `CREATE TABLE t (a int, b char(5)) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6),PARTITION p1 VALUES LESS THAN (11))`
testKit.MustExec(createTable)
do := s.do
is := do.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tableInfo := tbl.Meta()
h := do.StatsHandle()
err = h.HandleDDLEvent(<-h.DDLEventCh())
c.Assert(err, IsNil)
pi := tableInfo.GetPartitionInfo()
c.Assert(len(pi.Definitions), Equals, 2)
bColID := tableInfo.Columns[1].ID

testKit.MustExec(`insert into t values (1, "a"), (7, "a")`)
c.Assert(h.DumpStatsDeltaToKV(statistics.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
for _, def := range pi.Definitions {
statsTbl := h.GetPartitionStats(tableInfo, def.ID)
c.Assert(statsTbl.ModifyCount, Equals, int64(1))
c.Assert(statsTbl.Count, Equals, int64(1))
c.Assert(statsTbl.Columns[bColID].TotColSize, Equals, int64(1))
}

testKit.MustExec(`update t set a = a + 1, b = "aa"`)
c.Assert(h.DumpStatsDeltaToKV(statistics.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
for _, def := range pi.Definitions {
statsTbl := h.GetPartitionStats(tableInfo, def.ID)
c.Assert(statsTbl.ModifyCount, Equals, int64(2))
c.Assert(statsTbl.Count, Equals, int64(1))
c.Assert(statsTbl.Columns[bColID].TotColSize, Equals, int64(2))
}

testKit.MustExec("delete from t")
c.Assert(h.DumpStatsDeltaToKV(statistics.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
for _, def := range pi.Definitions {
statsTbl := h.GetPartitionStats(tableInfo, def.ID)
c.Assert(statsTbl.ModifyCount, Equals, int64(3))
c.Assert(statsTbl.Count, Equals, int64(0))
c.Assert(statsTbl.Columns[bColID].TotColSize, Equals, int64(0))
}
}

func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
Expand Down
18 changes: 17 additions & 1 deletion table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ func (t *tableCommon) UpdateRecord(ctx sessionctx.Context, h int64, oldData, new
return errors.Trace(err)
}
}
colSize := make(map[int64]int64)
for id, col := range t.Cols() {
val := int64(len(newData[id].GetBytes()) - len(oldData[id].GetBytes()))
if val != 0 {
colSize[col.ID] = val
}
}
ctx.GetSessionVars().TxnCtx.UpdateDeltaForTable(t.physicalTableID, 0, 1, colSize)
return nil
}

Expand Down Expand Up @@ -500,7 +508,7 @@ func (t *tableCommon) AddRecord(ctx sessionctx.Context, r []types.Datum, skipHan
colSize[col.ID] = val
}
}
sessVars.TxnCtx.UpdateDeltaForTable(t.tableID, 1, 1, colSize)
sessVars.TxnCtx.UpdateDeltaForTable(t.physicalTableID, 1, 1, colSize)
return recordID, nil
}

Expand Down Expand Up @@ -661,6 +669,14 @@ func (t *tableCommon) RemoveRecord(ctx sessionctx.Context, h int64, r []types.Da
}
err = t.addDeleteBinlog(ctx, binlogRow, colIDs)
}
colSize := make(map[int64]int64)
for id, col := range t.Cols() {
val := -int64(len(r[id].GetBytes()))
if val != 0 {
colSize[col.ID] = val
}
}
ctx.GetSessionVars().TxnCtx.UpdateDeltaForTable(t.physicalTableID, -1, 1, colSize)
return errors.Trace(err)
}

Expand Down

0 comments on commit ec9672c

Please sign in to comment.