TravelMapping / DataProcessing

Data Processing Scripts and Programs for Travel Mapping Project
4 stars 6 forks source link

Speeding up .sql file import #620

Open jteresco opened 7 months ago

jteresco commented 7 months ago

As I was waiting for last night's site update to read the big .sql file produced by the now very speedy C++ siteupdate program into mysql, I asked ChatGPT for ways that could be sped up. Here's what it came up with. Some are definitely not options for us but there could be good possibilities here. I don't have time in the next month or so to look into any of it, but will look back here when time permits. Others are welcome to experiment of course.

Loading a large .sql file into a MySQL database can be time-consuming, but there are several strategies you can employ to make the process faster:

    Use MySQL CLI: Instead of using a GUI tool, use the MySQL command-line interface (mysql) to load the .sql file. This often tends to be faster.

    Split the File: If the .sql file is extremely large, consider splitting it into smaller files. This can help reduce the load time. You can split it based on tables or logical divisions.

    Disable Foreign Key Checks: If your .sql file contains foreign key constraints, consider disabling foreign key checks during the import process. This can significantly speed up the import, especially if the tables are interrelated.

SET foreign_key_checks = 0;
-- Import SQL file
SET foreign_key_checks = 1;

Use MySQL Dump with Options: If the .sql file was generated by mysqldump, ensure it was generated with options that can speed up import, such as --single-transaction and --quick.

Increase Buffer Sizes: Adjust MySQL server configuration parameters like max_allowed_packet and innodb_buffer_pool_size to accommodate the large import.

Use Parallel Loading: If you have a multi-core system, you can split the import into parallel threads. However, be cautious with this approach as it may overload the server.

Disable Indexes and Triggers: Temporarily disabling indexes and triggers during the import process can speed up the loading time. You can rebuild them after the import is complete.

Use Compression: Compress the .sql file before importing it into the MySQL database. This can reduce the file size and speed up the transfer process.

Optimize SQL Queries: If you have control over the SQL file, optimize the queries to reduce the execution time. This includes optimizing joins, reducing unnecessary operations, and ensuring that indexes are used efficiently.

Use Load Data Infile: If your data is in a structured format such as CSV, you can use the LOAD DATA INFILE command, which can be faster than executing SQL INSERT statements.

Upgrade Hardware/Resources: If possible, consider upgrading your server's hardware or allocating more resources to the MySQL server to improve performance during the import process.
yakra commented 7 months ago

Use Load Data Infile: If your data is in a structured format such as CSV...

I remember looking into some of this when first getting lab2 off the ground, c. June 2019 IIRC, and this option looked intriguing. Posted some links on the forum at the time. They may or may not be relevant; may or may not even be alive now...