テーブルを一時的にロックして、他の同時プロセスが変更を加えないようにしたいと考えています。これは、このテーブルが一時テーブルにコピーされ、変更されてからコピーされるためです (元のテーブルは実際には削除され、新しいテーブルの名前が変更されます)。次に、これがすべて完了したら、テーブルのロックを解除し、ロック中に試行されたものを再開したいと考えています。
また、新しいテーブルを構築するためにロックされたテーブルから読み取ることができる必要があります。
これが私が試したものですが、うまくいかないようです。
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_PORT);
$mysqli->autocommit(false)
// create a table to hold parts to keep
$q1 = "CREATE TABLE new_table LIKE my_table;";
$res1 = $mysqli ->query($q1);
// Lock table to avoid concurrent update issues
$q2 = "LOCK TABLE my_table WRITE;";
$res2 = $mysqli ->query($q2);
// Insert data to keep into new table
$q3 = "INSERT INTO new_table SELECT * FROM my_table WHERE some_id IN (SELECT ID FROM table2)";
$res3 = $mysqli ->query($q3);
// drop original table
$q4 = "DROP TABLE my_table;";
$res4 = $mysqli ->query($q4);
// rename new table
$q5 = "RENAME TABLE new_table TO my_table;";
$res5 = $mysqli ->query($q5);
$mysqli ->commit(); // commit changes and re-enable autocommit
$q = "UNLOCK TABLES;";
$res = $mysqli ->query($q);
PHPmyadmin を使用してテストするために、「SET AUTOCOMMIT=0; LOCK TABLE my_table WRITE;」を発行しました。クエリを実行してから、my_table から何かを削除しようとしたところ、削除できました。これをブロックしたい。また、lock ステートメントを発行した後、残りの手順は失敗し、何も変更されません。