-
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
stats: fix dump stats #6285
stats: fix dump stats #6285
Changes from 3 commits
29cf535
eb2f00a
9b97c1c
e28e3d6
a7bdfed
c4b36e0
f738cfd
9b2ec40
a44427e
2e1dc24
a77be31
db9ef3d
c71b8f2
e9b8abd
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 |
---|---|---|
|
@@ -113,7 +113,8 @@ func (e *LoadStatsInfo) Update(data []byte) error { | |
Cms: cms, | ||
Count: tbl.Count, | ||
IsIndex: 0, | ||
Err: nil} | ||
Err: nil, | ||
} | ||
|
||
hists = make([]*statistics.Histogram, 0, len(tbl.Indices)) | ||
cms = make([]*statistics.CMSketch, 0, len(tbl.Indices)) | ||
|
@@ -127,9 +128,10 @@ func (e *LoadStatsInfo) Update(data []byte) error { | |
Cms: cms, | ||
Count: tbl.Count, | ||
IsIndex: 1, | ||
Err: nil} | ||
|
||
return nil | ||
Err: nil, | ||
} | ||
err = statistics.SaveMetaToStorage(e.Ctx, tbl.TableID, tbl.Count, tbl.ModifyCount, tbl.Version) | ||
return errors.Trace(err) | ||
} | ||
for _, col := range tbl.Columns { | ||
err = statistics.SaveStatsToStorage(e.Ctx, tbl.TableID, tbl.Count, 0, &col.Histogram, col.CMSketch) | ||
|
@@ -143,6 +145,10 @@ func (e *LoadStatsInfo) Update(data []byte) error { | |
return errors.Trace(err) | ||
} | ||
} | ||
err = statistics.SaveMetaToStorage(e.Ctx, tbl.TableID, tbl.Count, tbl.ModifyCount, tbl.Version) | ||
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. Why save it twice? 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 don't save it twice. They are two situations, and it will return immediately after the previous call. |
||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
err = h.Update(GetInfoSchema(e.Ctx)) | ||
return errors.Trace(err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ import ( | |
"github.com/pingcap/tidb/util/codec" | ||
"github.com/pingcap/tidb/util/ranger" | ||
"github.com/pingcap/tidb/util/sqlexec" | ||
tipb "github.com/pingcap/tipb/go-tipb" | ||
"github.com/pingcap/tipb/go-tipb" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
|
@@ -247,6 +247,25 @@ func SaveStatsToStorage(sctx sessionctx.Context, tableID int64, count int64, isI | |
return errors.Trace(err) | ||
} | ||
|
||
// SaveMetaToStorage will save stats_meta to storage. | ||
func SaveMetaToStorage(sctx sessionctx.Context, tableID, count, modifyCount int64, version uint64) error { | ||
ctx := context.TODO() | ||
exec := sctx.(sqlexec.SQLExecutor) | ||
_, err := exec.Execute(ctx, "begin") | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
var sql string | ||
sql = fmt.Sprintf("replace into mysql.stats_meta (version, table_id, count, modify_count) values (%d, %d, %d, %d)", version, tableID, count, modifyCount) | ||
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. We cannot use the original version, it may not be loaded into stats cache. |
||
if _, err = exec.Execute(ctx, sql); err != nil { | ||
_, err1 := exec.Execute(ctx, "rollback") | ||
terror.Log(errors.Trace(err1)) | ||
return errors.Trace(err) | ||
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. Maybe we should execute |
||
} | ||
_, err = exec.Execute(ctx, "commit") | ||
return errors.Trace(err) | ||
} | ||
|
||
func histogramFromStorage(ctx sessionctx.Context, tableID int64, colID int64, tp *types.FieldType, distinct int64, isIndex int, ver uint64, nullCount int64, totColSize int64) (*Histogram, error) { | ||
selSQL := fmt.Sprintf("select count, repeats, lower_bound, upper_bound from mysql.stats_buckets where table_id = %d and is_index = %d and hist_id = %d order by bucket_id", tableID, isIndex, colID) | ||
rows, fields, err := ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(ctx, selSQL) | ||
|
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 if err is nil, we should not trace it.
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.
If err is nil, the errors.Trace will return nil.