diff --git a/go/logic/applier.go b/go/logic/applier.go index 990fbe720..ec5a432cc 100644 --- a/go/logic/applier.go +++ b/go/logic/applier.go @@ -1226,6 +1226,23 @@ func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent)) return nil } +// AnalyzeTable actively analyze table to ensure that the ghost table's statistics are timely updated +func (this *Applier) AnalyzeTable() { + query := fmt.Sprintf(`analyze table /* gh-ost */ %s.%s`, + sql.EscapeName(this.migrationContext.DatabaseName), + sql.EscapeName(this.migrationContext.GetGhostTableName()), + ) + + this.migrationContext.Log.Infof("Analyzing ghost table %s.%s", + sql.EscapeName(this.migrationContext.DatabaseName), + sql.EscapeName(this.migrationContext.GetGhostTableName()), + ) + if _, err := sqlutils.ExecNoPrepare(this.db, query); err != nil { + this.migrationContext.Log.Warningf("Ghost table analyzes failed") + } + this.migrationContext.Log.Infof("Ghost table analyzed") +} + func (this *Applier) Teardown() { this.migrationContext.Log.Debugf("Tearing down...") this.db.Close() diff --git a/go/logic/migrator.go b/go/logic/migrator.go index 3fc897cd3..122a4b69a 100644 --- a/go/logic/migrator.go +++ b/go/logic/migrator.go @@ -424,6 +424,9 @@ func (this *Migrator) Migrate() (err error) { } this.printStatus(ForcePrintStatusRule) + // analyze table before cutover + this.applier.AnalyzeTable() + if this.migrationContext.IsCountingTableRows() { this.migrationContext.Log.Info("stopping query for exact row count, because that can accidentally lock out the cut over") this.migrationContext.CancelTableRowsCount()