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

Support fast_analyze_table variable, introduced in public MySQL fork #14494

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions go/vt/vttablet/onlineddl/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ const (
sqlShowVariablesLikePreserveForeignKey = "show global variables like 'rename_table_preserve_foreign_key'"
sqlEnablePreserveForeignKey = "set @@rename_table_preserve_foreign_key = 1"
sqlDisablePreserveForeignKey = "set @@rename_table_preserve_foreign_key = 0"
sqlShowVariablesLikeFastAnalyzeTable = "show global variables like 'fast_analyze_table'"
sqlEnableFastAnalyzeTable = "set @@fast_analyze_table = 1"
sqlDisableFastAnalyzeTable = "set @@fast_analyze_table = 0"
sqlGetAutoIncrement = `
SELECT
AUTO_INCREMENT
Expand Down
35 changes: 33 additions & 2 deletions go/vt/vttablet/onlineddl/vrepl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/textutil"
"vitess.io/vitess/go/vt/dbconnpool"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
Expand Down Expand Up @@ -247,11 +248,41 @@ func (v *VRepl) readTableUniqueKeys(ctx context.Context, conn *dbconnpool.DBConn
return uniqueKeys, nil
}

// isFastAnalyzeTableSupported checks if the underlying MySQL server supports 'fast_analyze_table',
// introduced by a fork of MySQL: https://github.com/planetscale/mysql-server/commit/c8a9d93686358dabfba8f3dc5cc0621e3149fe78
// When `fast_analyze_table=1`, an `ANALYZE TABLE` command only analyzes the clustering index (normally the `PRIMARY KEY`).
// This is useful when you want to get a better estimate of the number of table rows, as fast as possible.
func (v *VRepl) isFastAnalyzeTableSupported(ctx context.Context, conn *dbconnpool.DBConnection) (isSupported bool, err error) {
rs, err := conn.ExecuteFetch(sqlShowVariablesLikeFastAnalyzeTable, math.MaxInt64, true)
if err != nil {
return false, err
}
return len(rs.Rows) > 0, nil
}

// executeAnalyzeTable runs an ANALYZE TABLE command
func (v *VRepl) executeAnalyzeTable(ctx context.Context, conn *dbconnpool.DBConnection, tableName string) error {
fastAnalyzeTableSupported, err := v.isFastAnalyzeTableSupported(ctx, conn)
if err != nil {
return err
}
if fastAnalyzeTableSupported {
// This code is only applicable when MySQL supports the 'fast_analyze_table' variable. This variable
// does not exist in vanilla MySQL.
// See https://github.com/planetscale/mysql-server/commit/c8a9d93686358dabfba8f3dc5cc0621e3149fe78
// as part of https://github.com/planetscale/mysql-server/releases/tag/8.0.34-ps1.
if _, err := conn.ExecuteFetch(sqlEnableFastAnalyzeTable, 1, false); err != nil {
return err
}
log.Infof("@@fast_analyze_table enabled")
defer conn.ExecuteFetch(sqlDisableFastAnalyzeTable, 1, false)
}

parsed := sqlparser.BuildParsedQuery(sqlAnalyzeTable, tableName)
_, err := conn.ExecuteFetch(parsed.Query, 1, false)
return err
if _, err := conn.ExecuteFetch(parsed.Query, 1, false); err != nil {
return err
}
return nil
}

// readTableStatus reads table status information
Expand Down
Loading