From 653a95125d71fb887752f347e73a0500087c9f82 Mon Sep 17 00:00:00 2001 From: wangzihuacool Date: Thu, 23 May 2024 12:38:42 +0000 Subject: [PATCH] analyze table before cutover --- go/logic/applier.go | 17 +++++++++++++++++ go/logic/migrator.go | 3 +++ 2 files changed, 20 insertions(+) diff --git a/go/logic/applier.go b/go/logic/applier.go index 9b190919f..85c07c89c 100644 --- a/go/logic/applier.go +++ b/go/logic/applier.go @@ -1196,6 +1196,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 fed7c944b..4ea36f407 100644 --- a/go/logic/migrator.go +++ b/go/logic/migrator.go @@ -420,6 +420,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()