-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
*: update stats using query feedback #6197
Changes from 4 commits
14a8e31
f97232b
488ef40
1a4c15e
9dbe5bc
2cee59c
a5c1816
4660d8d
fe9054a
dafd48c
3e4ac52
4eca937
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -632,6 +632,10 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) | |
defer loadHistogramTicker.Stop() | ||
gcStatsTicker := time.NewTicker(100 * lease) | ||
defer gcStatsTicker.Stop() | ||
dumpFeedbackTicker := time.NewTicker(200 * lease) | ||
defer dumpFeedbackTicker.Stop() | ||
loadFeedbackTicker := time.NewTicker(5 * lease) | ||
defer loadFeedbackTicker.Stop() | ||
statsHandle := do.StatsHandle() | ||
t := time.Now() | ||
err := statsHandle.InitStats(do.InfoSchema()) | ||
|
@@ -644,7 +648,7 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) | |
for { | ||
select { | ||
case <-loadTicker.C: | ||
err := statsHandle.Update(do.InfoSchema()) | ||
err = statsHandle.Update(do.InfoSchema()) | ||
if err != nil { | ||
log.Error("[stats] update stats info fail: ", errors.ErrorStack(err)) | ||
} | ||
|
@@ -653,32 +657,45 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) | |
return | ||
// This channel is sent only by ddl owner or the drop stats executor. | ||
case t := <-statsHandle.DDLEventCh(): | ||
err := statsHandle.HandleDDLEvent(t) | ||
err = statsHandle.HandleDDLEvent(t) | ||
if err != nil { | ||
log.Error("[stats] handle ddl event fail: ", errors.ErrorStack(err)) | ||
} | ||
case t := <-statsHandle.AnalyzeResultCh(): | ||
for i, hg := range t.Hist { | ||
err := statistics.SaveStatsToStorage(ctx, t.TableID, t.Count, t.IsIndex, hg, t.Cms[i]) | ||
err = statistics.SaveStatsToStorage(ctx, t.TableID, t.Count, t.IsIndex, hg, t.Cms[i]) | ||
if err != nil { | ||
log.Error("[stats] save histogram to storage fail: ", errors.ErrorStack(err)) | ||
} | ||
} | ||
case <-deltaUpdateTicker.C: | ||
err := statsHandle.DumpStatsDeltaToKV() | ||
err = statsHandle.DumpStatsDeltaToKV() | ||
if err != nil { | ||
log.Error("[stats] dump stats delta fail: ", errors.ErrorStack(err)) | ||
} | ||
case <-loadHistogramTicker.C: | ||
err := statsHandle.LoadNeededHistograms() | ||
err = statsHandle.LoadNeededHistograms() | ||
if err != nil { | ||
log.Error("[stats] load histograms fail: ", errors.ErrorStack(err)) | ||
} | ||
case <-loadFeedbackTicker.C: | ||
if !owner.IsOwner() { | ||
continue | ||
} | ||
err = statsHandle.HandleUpdateStats(do.InfoSchema()) | ||
if err != nil { | ||
log.Errorf("[stats] update stats using feedback fail: ", errors.ErrorStack(err)) | ||
} | ||
case <-dumpFeedbackTicker.C: | ||
err = statsHandle.DumpStatsFeedbackToKV() | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will do it in a separate pr. |
||
log.Error("[stats] dump stats feedback fail: ", errors.ErrorStack(err)) | ||
} | ||
case <-gcStatsTicker.C: | ||
if !owner.IsOwner() { | ||
continue | ||
} | ||
err := statsHandle.GCStats(do.InfoSchema(), do.DDL().GetLease()) | ||
err = statsHandle.GCStats(do.InfoSchema(), do.DDL().GetLease()) | ||
if err != nil { | ||
log.Error("[stats] gc stats fail: ", errors.ErrorStack(err)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,7 +243,7 @@ func (s *testSuite) TestAggregation(c *C) { | |
|
||
result = tk.MustQuery("select count(*) from information_schema.columns") | ||
// When adding new memory columns in information_schema, please update this variable. | ||
columnCountOfAllInformationSchemaTables := "737" | ||
columnCountOfAllInformationSchemaTables := "741" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 column added instead of 4? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is 4 column added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, i misread it. |
||
result.Check(testkit.Rows(columnCountOfAllInformationSchemaTables)) | ||
|
||
tk.MustExec("drop table if exists t1") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ import ( | |
"github.com/pingcap/tidb/sessionctx/stmtctx" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/codec" | ||
tipb "github.com/pingcap/tipb/go-tipb" | ||
"github.com/pingcap/tipb/go-tipb" | ||
"github.com/spaolacci/murmur3" | ||
) | ||
|
||
|
@@ -54,6 +54,18 @@ func (c *CMSketch) InsertBytes(bytes []byte) { | |
} | ||
} | ||
|
||
// setValue sets the count for value that hashed into (h1, h2). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (h1, h2) is an interval? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it means the hash value pair. |
||
func (c *CMSketch) setValue(h1, h2 uint64, count uint32) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment for this function. |
||
oriCount := c.queryHashValue(h1, h2) | ||
c.count += uint64(count) - uint64(oriCount) | ||
// let it overflow naturally | ||
deltaCount := count - oriCount | ||
for i := range c.table { | ||
j := (h1 + h2*uint64(i)) % uint64(c.width) | ||
c.table[i][j] = c.table[i][j] + deltaCount | ||
} | ||
} | ||
|
||
func (c *CMSketch) queryValue(sc *stmtctx.StatementContext, val types.Datum) (uint32, error) { | ||
bytes, err := codec.EncodeValue(sc, nil, val) | ||
if err != nil { | ||
|
@@ -64,6 +76,10 @@ func (c *CMSketch) queryValue(sc *stmtctx.StatementContext, val types.Datum) (ui | |
|
||
func (c *CMSketch) queryBytes(bytes []byte) uint32 { | ||
h1, h2 := murmur3.Sum128(bytes) | ||
return c.queryHashValue(h1, h2) | ||
} | ||
|
||
func (c *CMSketch) queryHashValue(h1, h2 uint64) uint32 { | ||
vals := make([]uint32, c.depth) | ||
min := uint32(math.MaxUint32) | ||
for i := range c.table { | ||
|
@@ -173,3 +189,15 @@ func (c *CMSketch) Equal(rc *CMSketch) bool { | |
} | ||
return true | ||
} | ||
|
||
func (c *CMSketch) copy() *CMSketch { | ||
if c == nil { | ||
return nil | ||
} | ||
tbl := make([][]uint32, c.depth) | ||
for i := range tbl { | ||
tbl[i] = make([]uint32, c.width) | ||
copy(tbl[i], c.table[i]) | ||
} | ||
return &CMSketch{count: c.count, width: c.width, depth: c.depth, table: tbl} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a metrics here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to add metrics in a separate pr, it is already too large now.