Middle+
What to do if a database in an SQL file with a million 155 thousand rows cannot be inserted using tools?
sobes.tech AI
Answer from AI
If standard tools (phpMyAdmin, Adminer, terminal utilities like mysql) cannot handle importing an SQL file of such size, there are several main strategies:
-
Using terminal utilities:
- The most reliable method for large files.
- Directly invoke the import command, bypassing the web server and PHP limitations.
# For MySQL mysql -u your_user -p your_database < your_file.sql # For PostgreSQL psql -U your_user -d your_database -f your_file.sql -
Splitting the file into parts:
- A very large file may exceed memory or execution time limits.
- The file can be split manually, with scripts, or specialized tools.
- Import each part sequentially.
-
Configuring the web server and PHP:
- Increase the following parameters in
php.ini(for phpMyAdmin/Adminer):upload_max_filesizepost_max_sizememory_limitmax_execution_timemax_input_time
- Increase
client_max_body_sizein Nginx configuration orLimitRequestBodyin Apache.
- Increase the following parameters in
-
Optimizing the SQL file:
- Remove unnecessary comments or empty lines.
- Change the format of
INSERTstatements from multiple rows to a singleINSERTwith multiple value sets (if the file format allows and the database supports it), for example:-- Instead of: -- INSERT INTO table (col1, col2) VALUES (1, 'a'); -- INSERT INTO table (col1, col2) VALUES (2, 'b'); -- Use: -- INSERT INTO table (col1, col2) VALUES -- (1, 'a'), -- (2, 'b');
-
Using specialized tools:
- There are utilities designed specifically for processing large SQL dumps, such as
mydumper/myloaderfor MySQL.
- There are utilities designed specifically for processing large SQL dumps, such as
-
Server-side import:
- If you have access to the database server's filesystem, you can use commands like
LOAD DATA INFILE(MySQL) orCOPY(PostgreSQL), but this requires changing the file format to CSV or TAB-separated.
-- Example for MySQL with LOAD DATA INFILE (requires CSV or other delimiter format) -- LOAD DATA INFILE '/path/to/your/file.csv' -- INTO TABLE your_table -- FIELDS TERMINATED BY ',' ENCLOSED BY '"' -- LINES TERMINATED BY '\n'; - If you have access to the database server's filesystem, you can use commands like
The choice of method depends on available resources (direct server access, configuration change capabilities) and technical expertise. Generally, using terminal utilities or splitting the file are the most reliable solutions for large data volumes.