diff --git a/br/pkg/restore/client.go b/br/pkg/restore/client.go index ae33055dec0f5..0da911debb1ae 100644 --- a/br/pkg/restore/client.go +++ b/br/pkg/restore/client.go @@ -377,7 +377,9 @@ func (rc *Client) InitCheckpointMetadataForLogRestore(ctx context.Context, taskN return gcRatio, nil } -func (rc *Client) allocTableIDs(ctx context.Context, tables []*metautil.Table) error { +// AllocTableIDs would pre-allocate the table's origin ID if exists, so that the TiKV doesn't need to rewrite the key in +// the download stage. +func (rc *Client) AllocTableIDs(ctx context.Context, tables []*metautil.Table) error { rc.preallocedTableIDs = tidalloc.New(tables) ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnBR) err := kv.RunInNewTxn(ctx, rc.GetDomain().Store(), true, func(_ context.Context, txn kv.Transaction) error { @@ -955,11 +957,6 @@ func (rc *Client) GoCreateTables( } outCh := make(chan CreatedTable, len(tables)) rater := logutil.TraceRateOver(logutil.MetricTableCreatedCounter) - if err := rc.allocTableIDs(ctx, tables); err != nil { - errCh <- err - close(outCh) - return outCh - } var err error diff --git a/br/pkg/task/restore.go b/br/pkg/task/restore.go index 2c2f693567545..7482c4b6eeaa7 100644 --- a/br/pkg/task/restore.go +++ b/br/pkg/task/restore.go @@ -882,6 +882,12 @@ func runRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf } } + // preallocate the table id, because any ddl job or database creation also allocates the global ID + err = client.AllocTableIDs(ctx, tables) + if err != nil { + return errors.Trace(err) + } + // execute DDL first err = client.ExecDDLs(ctx, ddlJobs) if err != nil {