How to Bulk Load Data in MySQL

Galaxy Glossary

How do I bulk load data into MySQL quickly?

LOAD DATA INFILE lets you import large text files into MySQL tables in one statement, making bulk inserts dramatically faster than INSERT.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why choose LOAD DATA INFILE over many INSERTs?

LOAD DATA INFILE streams the entire file directly into the storage engine, bypassing SQL parsing for every row. This cuts network round-trips and reduces redo/undo overhead, resulting in imports that are 10-20× faster than loops of INSERT statements.

What file formats work best?

Plain CSV or TSV files exported from spreadsheets, ETL tools, or other databases import cleanly. Use the same delimiter, enclosure, and line-ending settings in both export and LOAD DATA INFILE to avoid parsing errors.

How do I point MySQL to the file location?

Use LOCAL if the file sits on the client machine:
LOAD DATA LOCAL INFILE '/path/to/orders.csv' ...
Omit LOCAL when the file is on the MySQL server and secure_file_priv allows the directory.

Which options should I always set?

Specify FIELDS TERMINATED BY ',' and ENCLOSED BY '"' for CSV, and match LINES TERMINATED BY '\n'. Include IGNORE 1 LINES to skip headers. Add SET created_at = NOW() to populate audit columns not present in the file.

Can I load into selected columns?

Yes—list the target columns after the table name. Columns omitted from the list take their default values, letting you ignore auto-increment IDs or computed fields.

How do I handle duplicate keys?

Add REPLACE to overwrite conflicting rows or IGNORE to skip them. Pick one; omitting both aborts the entire load on the first duplicate.

Is the load transaction-safe?

LOAD DATA INFILE obeys autocommit. Wrap it in START TRANSACTION / COMMIT to ensure an all-or-nothing import. This is crucial when loading into multiple related tables.

How do I verify performance?

Enable SET GLOBAL general_log = 'ON' temporarily and watch the rows-per-second metric. For million-row CSVs on SSDs, speeds of 50k–150k rows/sec are typical.

Best practices recap

Pre-disable secondary indexes, load from the same server disk, import into an empty staging table first, and always validate row counts. Re-enable indexes and run checksum comparisons before promoting data.

Why How to Bulk Load Data in MySQL is important

How to Bulk Load Data in MySQL Example Usage


-- Load products with current stock
LOAD DATA INFILE '/var/lib/mysql-files/products.csv'
REPLACE INTO TABLE Products
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, name, price, stock)
SET created_at = NOW();

How to Bulk Load Data in MySQL Syntax


LOAD {DATA | DATA LOCAL} INFILE 'file_path'
    [REPLACE | IGNORE]
    INTO TABLE table_name
    [CHARACTER SET charset_name]
    [FIELDS
        [TERMINATED BY 'string']
        [ENCLOSED BY 'char']
        [ESCAPED BY 'char'] ]
    [LINES TERMINATED BY 'string']
    [IGNORE n LINES]
    [(col1,col2, ...)]
    [SET col1 = expr1, col2 = expr2, ...];

-- Example: load Orders
LOAD DATA LOCAL INFILE '/tmp/orders.csv'
INTO TABLE Orders
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(order_id, customer_id, order_date, total_amount);

Common Mistakes

Frequently Asked Questions (FAQs)

Does LOAD DATA INFILE lock the table?

InnoDB places a metadata lock but allows concurrent reads unless you use REPLACE, which may escalate locks. For MyISAM, the table is write-locked for the duration.

Can I load gzip files directly?

No, decompress first. Use gunzip -c file.csv.gz | mysql -e "LOAD DATA LOCAL INFILE '/dev/stdin' ..." for one-liner imports.

How do I skip bad rows but keep the rest?

Add IGNORE. MySQL logs row errors to SHOW WARNINGS but continues loading the remaining data.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.