How do you perform a bulk insert of data into a table?
Posted by MaryJns
Last Updated: July 18, 2024
Performing a bulk insert of data into a table can be done using various methods, depending on the database management system (DBMS) you are using. Below are some common approaches for popular DBMSs like MySQL, PostgreSQL, SQL Server, and Oracle.
1. MySQL
In MySQL, you can use the LOAD DATA INFILE command to load data from a file directly into a table.
LOAD DATA INFILE '/path/to/yourfile.csv'
INTO TABLE your_table
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS; -- if you want to skip header
For inserting multiple rows at once, you can use the INSERT statement with multiple value tuples:
INSERT INTO your_table (column1, column2, column3)
VALUES
    (value1a, value2a, value3a),
    (value1b, value2b, value3b),
    (value1c, value2c, value3c);
2. PostgreSQL
For PostgreSQL, you can use the COPY command to perform a bulk insert from a file:
COPY your_table (column1, column2, column3)
FROM '/path/to/yourfile.csv'
DELIMITER ','
CSV HEADER; -- if you want to skip the header
Alternatively, you can also use the INSERT statement with multiple values:
INSERT INTO your_table (column1, column2, column3)
VALUES
    (value1a, value2a, value3a),
    (value1b, value2b, value3b),
    (value1c, value2c, value3c);
3. SQL Server
In SQL Server, you can use the BULK INSERT command to load data from a file:
BULK INSERT your_table
FROM 'C:\path\to\yourfile.csv'
WITH
(
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n',
    FIRSTROW = 2 -- if you want to skip header
);
You can also insert multiple rows using the INSERT statement:
INSERT INTO your_table (column1, column2, column3)
VALUES
    (value1a, value2a, value3a),
    (value1b, value2b, value3b),
    (value1c, value2c, value3c);
4. Oracle
In Oracle, you can use SQL*Loader to load data from flat files. Here's a simple command line example:
sqlldr userid=username/password control=your_control_file.ctl
The control file (your_control_file.ctl) defines the structure of the data being loaded. Like others, you can also perform bulk inserts with the INSERT ALL statement:
INSERT ALL
    INTO your_table (column1, column2, column3) VALUES (value1a, value2a, value3a)
    INTO your_table (column1, column2, column3) VALUES (value1b, value2b, value3b)
SELECT * FROM dual;
General Tips
- Transactions: When performing bulk inserts, consider wrapping your operations in a transaction to ensure data integrity. - Indexes: If inserting a large number of rows, consider temporarily disabling indexes or constraints to improve performance. - Batching: If you're inserting rows individually in a loop, consider batching your inserts to reduce the number of commits, which can improve performance. Always remember to consult the documentation of the specific DBMS you are using, as syntax and capabilities may vary.