Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL e bloccare una tabella, leggere e quindi troncare

Non è possibile troncare una tabella bloccata per la scrittura. Questo perché "troncare" significa "distruggere la tabella e ricrearne una nuova con lo stesso schema".

Tuttavia, puoi svuotare la tavola. Invece di TRUNCATE TABLE asin_one_time_only usa DELETE FROM asin_one_time_only . Si noti che questo non ripristinerà la numerazione di incremento automatico. Se vuoi resettarlo anche tu, usa ALTER TABLE asin_one_time_only auto_increment=1

Suggerisco di fare questo:

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;