4

これは私の前の質問へのフォローアップの質問です。

以下のコードを使用してデータベース テーブルを複製しますが、テーブル サイズが大きい場合、スクリプトがハングしてページが待機し続けることがあります。両方のテーブルの行数を比較すると、次のようなものが見つかります。

元のテーブル数: 855057
バックアップされたテーブル数: 855022

プロセスが完全に完了していないようで、挿入ステートメントの実行中にスタックしたのはなぜですか?

ほとんどの場合、両方とも実際には同じにカウントされますが、複製が終了してもハングすることに注意してください。

これが私のコードです:

//duplicate tables structure 
 $query = "CREATE TABLE $this->dbName.`$newTableName` LIKE $this->dbName.`$oldTable`";
    ..
    ..
    
//duplicate tables data
  $query = "INSERT INTO $this->dbName.`$newTableName` SELECT * FROM $this->dbName.`$oldTable`";
    ..
    ..

ps。ローカルホストから複製スクリプトを実行して、リモートサーバーにあるデータベースをバックアップします。

4

2 に答える 2

2

You will want to check if the query is still running by running show full processlist. You will also have to keep in mind that if the tables engine is innodb an undo log is also created as in case of interruption the transaction will have to rollback.

于 2012-12-26T21:13:31.787 に答える
1

ロックテーブルを使用する

// duplicate tables structure 
$query = "CREATE TABLE $this->dbName.`$newTableName` LIKE $this->dbName.`$oldTable`";

// lock source and target table
$query = "LOCK TABLES $this->dbName.`$newTableName` WRITE, $this->dbName.`$oldTable` AS source WRITE";

// duplicate tables data
$query = "INSERT INTO $this->dbName.`$newTableName` SELECT * FROM $this->dbName.`$oldTable` AS source";

// unlock
$query = "UNLOCK TABLES";
于 2012-12-26T21:41:35.610 に答える