Parse the delay insert option for mysqldump

  • 2020-05-24 06:22:48
  • OfStack

If you insert with delayed-insert, you do not need to lock the table.
./bin/mysqldump -uroot -proot --lock-tables --extended-insert --opt --quick --master-data test > /home/zhanghong/opdir/tmp/test.sql
--
LOCK TABLES `student` WRITE;
/*!40000 ALTER TABLE `student` DISABLE KEYS */;
INSERT INTO `student` VALUES (16,'hhah',3),(17,'22',3),(18,'ss',18);
/*!40000 ALTER TABLE `student` ENABLE KEYS */;
UNLOCK TABLES;

Using delayed-insert does not lock the table
./bin/mysqldump -uroot -proot --lock-tables --delayed-insert --extended-insert --opt --quick --master-data test > /home/zhanghong/opdir/tmp/test.sql
/*!40000 ALTER TABLE `student` DISABLE KEYS */;
INSERT DELAYED INTO `student` VALUES (16,'hhah',3),(17,'22',3),(18,'ss',18);
/*!40000 ALTER TABLE `student` ENABLE KEYS */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

When a thread executes an DELAYED statement on a table, a hypervisor thread (if one does not already exist) is created to process all DELAYED statements for this table.

· the thread checks to see if the hypervisor has previously acquired an DELAYED lock; If not, the hypervisor thread is notified to do so. Even if another thread has an READ or WRITE lock on the table, an DELAYED lock can be obtained. But the hypervisor waits for all ALTER TABLE locks or FLUSH TABLE locks to ensure that the structure of the table is up to date.

· the thread executes the INSERT statement, but instead of writing the rows to the table, it copies the final rows into a queue managed by the hypervisor thread. The thread prompts for syntax errors, which are reported to the client.

· because the return of INSERT was completed before the insert operation, the client cannot get the number of duplicate records from the server, nor the AUTO_INCREMENT value of the generated rows. (if you use C API, the mysql_info() function does not return anything meaningful for the same reason.)

· when a row is inserted into a table, the binary log is updated by the hypervisor thread. In the case of multi-row inserts, the binary log is updated when the first row is inserted.


Related articles: