Slow problem optimization in the process of MySQL importing SQL files

The slowness of MySQL importing SQL files may be caused by various reasons. Here are some common optimization suggestions:

  1. Disable autocommit : MySQL enables the autocommit mode by default. Every time an insert statement is executed, it will be automatically committed once, which seriously affects the import speed. You can turn off autocommit before the import starts: SET autocommit=0;, and turn it on after the import: SET autocommit=1;. Alternatively, start START TRANSACTION;a transaction and commit after the import is complete COMMIT;.

  2. Batch insertion : Try to insert data in batches instead of inserting them one by one. It can reduce the number of interactions and network delays, and improve the import speed.

  3. Adjust innodb_buffer_pool_size : InnoDB storage engine caches data in memory to improve read and write speed. If your machine has enough memory, you can increase this value appropriately. Generally speaking, it is more appropriate to set this value to 50%-70% of the total memory.

  4. Turn off index and key checking : When importing data, you can temporarily turn off index and foreign key checking, which can improve the import speed. Re-build the index and turn on foreign key checking after the import is complete. as follows:

    ALTER TABLE your_table DISABLE KEYS;
    SET FOREIGN_KEY_CHECKS=0;
    -- 导入数据
    ALTER TABLE your_table ENABLE KEYS;
    SET FOREIGN_KEY_CHECKS=1;
    
  5. Adjust write cache : When importing large files, you can increase the size of MySQL's write cache. You can modify the my.cnf file to increase innodb_log_buffer_sizethe value.

  6. Adjust max_allowed_packet : If you have very large BLOB data in your SQL file, you may need to increase the value of max_allowed_packet, otherwise MySQL may report an error.

  7. Compressed data : If your network bandwidth is limited, you can first compress the SQL file, then decompress it on the MySQL server and import it.

Note that while these optimizations may improve import speed, they may also affect other operations of your MySQL server. Therefore, before making any changes, it is necessary to assess the possible impact and ensure the ability to revert to the original settings.

Guess you like

Origin blog.csdn.net/u011197085/article/details/131496937