@@ -104,6 +104,8 @@ type VRepl struct {
104104 alterQuery string
105105 tableRows int64
106106
107+ analyzeTable bool
108+
107109 sourceSharedColumns * vrepl.ColumnList
108110 targetSharedColumns * vrepl.ColumnList
109111 droppedSourceNonGeneratedColumns * vrepl.ColumnList
@@ -130,7 +132,7 @@ type VRepl struct {
130132}
131133
132134// NewVRepl creates a VReplication handler for Online DDL
133- func NewVRepl (workflow , keyspace , shard , dbName , sourceTable , targetTable , alterQuery string ) * VRepl {
135+ func NewVRepl (workflow , keyspace , shard , dbName , sourceTable , targetTable , alterQuery string , analyzeTable bool ) * VRepl {
134136 return & VRepl {
135137 workflow : workflow ,
136138 keyspace : keyspace ,
@@ -139,6 +141,7 @@ func NewVRepl(workflow, keyspace, shard, dbName, sourceTable, targetTable, alter
139141 sourceTable : sourceTable ,
140142 targetTable : targetTable ,
141143 alterQuery : alterQuery ,
144+ analyzeTable : analyzeTable ,
142145 parser : vrepl .NewAlterTableParser (),
143146 enumToTextMap : map [string ]string {},
144147 intToEnumMap : map [string ]bool {},
@@ -226,6 +229,13 @@ func (v *VRepl) readTableUniqueKeys(ctx context.Context, conn *dbconnpool.DBConn
226229 return uniqueKeys , nil
227230}
228231
232+ // executeAnalyzeTable runs an ANALYZE TABLE command
233+ func (v * VRepl ) executeAnalyzeTable (ctx context.Context , conn * dbconnpool.DBConnection , tableName string ) error {
234+ parsed := sqlparser .BuildParsedQuery (sqlAnalyzeTable , tableName )
235+ _ , err := conn .ExecuteFetch (parsed .Query , 1 , false )
236+ return err
237+ }
238+
229239// readTableStatus reads table status information
230240func (v * VRepl ) readTableStatus (ctx context.Context , conn * dbconnpool.DBConnection , tableName string ) (tableRows int64 , err error ) {
231241 parsed := sqlparser .BuildParsedQuery (sqlShowTableStatus , tableName )
@@ -335,6 +345,11 @@ func (v *VRepl) analyzeAlter(ctx context.Context) error {
335345}
336346
337347func (v * VRepl ) analyzeTables (ctx context.Context , conn * dbconnpool.DBConnection ) (err error ) {
348+ if v .analyzeTable {
349+ if err := v .executeAnalyzeTable (ctx , conn , v .sourceTable ); err != nil {
350+ return err
351+ }
352+ }
338353 v .tableRows , err = v .readTableStatus (ctx , conn , v .sourceTable )
339354 if err != nil {
340355 return err
0 commit comments