-
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
statistics: add comments and remove unnessary check #53568
Merged
ti-chi-bot
merged 3 commits into
pingcap:master
from
Rustin170506:rustin-patch-disable-tracking
May 27, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,8 +101,8 @@ func LoadColumnStatsUsage(sctx sessionctx.Context, loc *time.Location) (map[mode | |
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
// If `last_used_at` is before the time when `set global enable_column_tracking = 0`, we should ignore it because | ||
// `set global enable_column_tracking = 0` indicates all the predicate columns collected before. | ||
// If `last_used_at` is before the time when `set global tidb_enable_column_tracking = 0`, we should ignore it because | ||
// `set global tidb_enable_column_tracking = 0` indicates all the predicate columns collected before. | ||
if disableTime == nil || gt.After(*disableTime) { | ||
t := types.NewTime(types.FromGoTime(gt.In(loc)), mysql.TypeTimestamp, types.DefaultFsp) | ||
statsUsage.LastUsedAt = &t | ||
|
@@ -123,26 +123,35 @@ func LoadColumnStatsUsage(sctx sessionctx.Context, loc *time.Location) (map[mode | |
|
||
// GetPredicateColumns returns IDs of predicate columns, which are the columns whose stats are used(needed) when generating query plans. | ||
func GetPredicateColumns(sctx sessionctx.Context, tableID int64) ([]int64, error) { | ||
// This time is the time when `set global tidb_enable_column_tracking = 0`. | ||
disableTime, err := getDisableColumnTrackingTime(sctx) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
rows, _, err := utilstats.ExecRows(sctx, "SELECT column_id, CONVERT_TZ(last_used_at, @@TIME_ZONE, '+00:00') FROM mysql.column_stats_usage WHERE table_id = %? AND last_used_at IS NOT NULL", tableID) | ||
rows, _, err := utilstats.ExecRows( | ||
sctx, | ||
"SELECT column_id, CONVERT_TZ(last_used_at, @@TIME_ZONE, '+00:00') FROM mysql.column_stats_usage WHERE table_id = %? AND last_used_at IS NOT NULL", | ||
tableID, | ||
) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
columnIDs := make([]int64, 0, len(rows)) | ||
for _, row := range rows { | ||
if row.IsNull(0) || row.IsNull(1) { | ||
// Usually, it should not be NULL. | ||
// This only happens when the last_used_at is not a valid time. | ||
if row.IsNull(1) { | ||
continue | ||
} | ||
colID := row.GetInt64(0) | ||
gt, err := row.GetTime(1).GoTime(time.UTC) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
// If `last_used_at` is before the time when `set global enable_column_tracking = 0`, we don't regard the column as predicate column because | ||
// `set global enable_column_tracking = 0` indicates all the predicate columns collected before. | ||
// If `last_used_at` is before the time when `set global tidb_enable_column_tracking = 0`, we don't regard the column as predicate column because | ||
// `set global tidb_enable_column_tracking = 0` indicates all the predicate columns collected before. | ||
// TODO: Why do we need to do this? If column tracking is already disabled, we should not collect any column usage. | ||
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 discuss it with roger. |
||
// If this refers to re-enabling column tracking, shouldn't we retain the column usage data from before it was disabled? | ||
if disableTime == nil || gt.After(*disableTime) { | ||
columnIDs = append(columnIDs, colID) | ||
} | ||
|
@@ -151,14 +160,22 @@ func GetPredicateColumns(sctx sessionctx.Context, tableID int64) ([]int64, error | |
} | ||
|
||
// getDisableColumnTrackingTime reads the value of tidb_disable_column_tracking_time from mysql.tidb if it exists. | ||
// UTC time format is used to store the time. | ||
func getDisableColumnTrackingTime(sctx sessionctx.Context) (*time.Time, error) { | ||
rows, fields, err := utilstats.ExecRows(sctx, "SELECT variable_value FROM %n.%n WHERE variable_name = %?", mysql.SystemDB, mysql.TiDBTable, variable.TiDBDisableColumnTrackingTime) | ||
rows, fields, err := utilstats.ExecRows( | ||
sctx, | ||
"SELECT variable_value FROM %n.%n WHERE variable_name = %?", | ||
mysql.SystemDB, | ||
mysql.TiDBTable, | ||
variable.TiDBDisableColumnTrackingTime, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(rows) == 0 { | ||
return nil, nil | ||
} | ||
|
||
d := rows[0].GetDatum(0, &fields[0].Column.FieldType) | ||
// The string represents the UTC time when tidb_enable_column_tracking is set to 0. | ||
value, err := d.ToString() | ||
|
@@ -169,6 +186,7 @@ func getDisableColumnTrackingTime(sctx sessionctx.Context) (*time.Time, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &t, nil | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Removed the first check here.