Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats: update delta info for partition table #7947

Merged
merged 3 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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