Mysql
 sql >> Base de données >  >> RDS >> Mysql

MySQL et verrouillez une table, lisez, puis tronquez

Vous ne pouvez pas tronquer un tableau verrouillé en écriture. En effet, "tronquer" signifie "détruire la table et en recréer une nouvelle avec le même schéma."

Vous pouvez cependant, vider la table. Au lieu de TRUNCATE TABLE asin_one_time_only utilisez DELETE FROM asin_one_time_only . Notez que cela ne réinitialisera pas la numérotation d'auto-incrémentation. Si vous souhaitez également le réinitialiser, utilisez ALTER TABLE asin_one_time_only auto_increment=1

Je suggère de faire ceci :

LOCK TABLES asin_one_time_only READ;
SELECT asin FROM asin_one_time_only;
-- minimize the possibility of someone writing to the table in-between
-- an "UNLOCK TABLES" and a "LOCK TABLES" by just issuing a new LOCK TABLES
-- I am not 100% sure that MySQL will do this atomically, so there is a
-- possibility that you may delete a row that was not read.
-- If this is unacceptable, then use a "LOCK TABLES asin_one_time_only WRITE"
-- from the very beginning.
LOCK TABLES asin_one_time_only WRITE;
DELETE FROM asin_one_time_only;
ALTER TABLE asin_one_time_only auto_increment=1;
UNLOCK TABLES;