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

Online DDL: improved row estimation via ANALYE TABLE with --analyze-table strategy flag #13352

Merged
merged 5 commits into from
Jul 13, 2023
Merged
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
Prev Previous commit
Next Next commit
Issue an ANALYZE TABLE if --analyze-table is given. Also, update tabl…
…e_rows if rows_copied exceeds it

Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
shlomi-noach committed Jun 21, 2023

Verified

This commit was signed with the committer’s verified signature.
shlomi-noach Shlomi Noach
commit 31aaafb272a074922d3df86b8ff527913f465256
5 changes: 3 additions & 2 deletions go/vt/vttablet/onlineddl/executor.go
Original file line number Diff line number Diff line change
@@ -1316,7 +1316,7 @@ func (e *Executor) initVreplicationOriginalMigration(ctx context.Context, online
}
}
}
v = NewVRepl(onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, onlineDDL.SQL)
v = NewVRepl(onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, onlineDDL.SQL, onlineDDL.StrategySetting().IsAnalyzeTableFlag())
return v, nil
}

@@ -1370,7 +1370,7 @@ func (e *Executor) initVreplicationRevertMigration(ctx context.Context, onlineDD
if err := e.updateArtifacts(ctx, onlineDDL.UUID, vreplTableName); err != nil {
return v, err
}
v = NewVRepl(onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, "")
v = NewVRepl(onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, "", false)
v.pos = revertStream.pos
return v, nil
}
@@ -4207,6 +4207,7 @@ func (e *Executor) updateMigrationProgress(ctx context.Context, uuid string, pro

func (e *Executor) updateMigrationProgressByRowsCopied(ctx context.Context, uuid string, rowsCopied int64) error {
query, err := sqlparser.ParseAndBind(sqlUpdateMigrationProgressByRowsCopied,
sqltypes.Int64BindVariable(rowsCopied),
sqltypes.Int64BindVariable(rowsCopied),
sqltypes.StringBindVariable(uuid),
)
2 changes: 2 additions & 0 deletions go/vt/vttablet/onlineddl/schema.go
Original file line number Diff line number Diff line change
@@ -213,6 +213,7 @@ const (
`
sqlUpdateMigrationProgressByRowsCopied = `UPDATE _vt.schema_migrations
SET
table_rows=GREATEST(table_rows, %a),
progress=CASE
WHEN table_rows=0 THEN 100
ELSE LEAST(100, 100*%a/table_rows)
@@ -520,6 +521,7 @@ const (
sqlDropTableIfExists = "DROP TABLE IF EXISTS `%a`"
sqlShowColumnsFrom = "SHOW COLUMNS FROM `%a`"
sqlShowTableStatus = "SHOW TABLE STATUS LIKE '%a'"
sqlAnalyzeTable = "ANALYZE TABLE `%a`"
sqlShowCreateTable = "SHOW CREATE TABLE `%a`"
sqlGetAutoIncrement = `
SELECT
17 changes: 16 additions & 1 deletion go/vt/vttablet/onlineddl/vrepl.go
Original file line number Diff line number Diff line change
@@ -104,6 +104,8 @@ type VRepl struct {
alterQuery string
tableRows int64

analyzeTable bool

sourceSharedColumns *vrepl.ColumnList
targetSharedColumns *vrepl.ColumnList
droppedSourceNonGeneratedColumns *vrepl.ColumnList
@@ -130,7 +132,7 @@ type VRepl struct {
}

// NewVRepl creates a VReplication handler for Online DDL
func NewVRepl(workflow, keyspace, shard, dbName, sourceTable, targetTable, alterQuery string) *VRepl {
func NewVRepl(workflow, keyspace, shard, dbName, sourceTable, targetTable, alterQuery string, analyzeTable bool) *VRepl {
return &VRepl{
workflow: workflow,
keyspace: keyspace,
@@ -139,6 +141,7 @@ func NewVRepl(workflow, keyspace, shard, dbName, sourceTable, targetTable, alter
sourceTable: sourceTable,
targetTable: targetTable,
alterQuery: alterQuery,
analyzeTable: analyzeTable,
parser: vrepl.NewAlterTableParser(),
enumToTextMap: map[string]string{},
intToEnumMap: map[string]bool{},
@@ -226,6 +229,13 @@ func (v *VRepl) readTableUniqueKeys(ctx context.Context, conn *dbconnpool.DBConn
return uniqueKeys, nil
}

// executeAnalyzeTable runs an ANALYZE TABLE command
func (v *VRepl) executeAnalyzeTable(ctx context.Context, conn *dbconnpool.DBConnection, tableName string) error {
parsed := sqlparser.BuildParsedQuery(sqlAnalyzeTable, tableName)
_, err := conn.ExecuteFetch(parsed.Query, 1, false)
return err
}

// readTableStatus reads table status information
func (v *VRepl) readTableStatus(ctx context.Context, conn *dbconnpool.DBConnection, tableName string) (tableRows int64, err error) {
parsed := sqlparser.BuildParsedQuery(sqlShowTableStatus, tableName)
@@ -335,6 +345,11 @@ func (v *VRepl) analyzeAlter(ctx context.Context) error {
}

func (v *VRepl) analyzeTables(ctx context.Context, conn *dbconnpool.DBConnection) (err error) {
if v.analyzeTable {
if err := v.executeAnalyzeTable(ctx, conn, v.sourceTable); err != nil {
return err
}
}
v.tableRows, err = v.readTableStatus(ctx, conn, v.sourceTable)
if err != nil {
return err