DBAtlas · Database operations atlas

Bulk load data (CSV / from file)

Oracle

-- SQL*Loader (from the shell)
sqlldr app_user/password control=customer.ctl log=load.log

-- Contents of customer.ctl:
-- LOAD DATA INFILE 'customer.csv'
-- INTO TABLE customer FIELDS TERMINATED BY ','
-- (id, name, email)

-- Alternative: with an external table
CREATE TABLE ext_customer (id NUMBER, name VARCHAR2(100))
ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY dir_csv
  ACCESS PARAMETERS (FIELDS TERMINATED BY ',') LOCATION ('customer.csv'));

The most common tool is SQL*Loader (with a control file). To read the file like a query, use an external table. For large volumes, direct path load (DIRECT=TRUE) is fast.

PostgreSQL

-- Server side (file on the server; requires superuser/pg_read_server_files)
COPY customer (id, name, email) FROM '/data/customer.csv' WITH (FORMAT csv, HEADER true);

-- Client side (psql, file on the client)
\copy customer (id, name, email) FROM 'customer.csv' WITH (FORMAT csv, HEADER true)

COPY reads a file on the server and requires privileges; psql \copy reads a client file and needs no extra privileges. It is the fastest way to load CSV.

MySQL

-- Server side
LOAD DATA INFILE '/data/customer.csv' INTO TABLE customer
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;

-- From a client file
LOAD DATA LOCAL INFILE 'customer.csv' INTO TABLE customer
FIELDS TERMINATED BY ',' IGNORE 1 LINES;

LOAD DATA INFILE is the fastest way to bulk load. LOCAL reads a client file and must be enabled both on the server (local_infile=ON) and on the client. secure_file_priv restricts the server path.

SQL Server

-- T-SQL BULK INSERT
BULK INSERT customer FROM 'C:\data\customer.csv'
WITH (FORMAT='CSV', FIRSTROW=2, FIELDTERMINATOR=',', ROWTERMINATOR='0x0a');

-- bcp from the shell
bcp mydb.dbo.customer in customer.csv -c -t, -S server -T

BULK INSERT reads the file from a path accessible to the SQL Server service account. bcp is a command-line tool; -c is character mode, -t the field terminator. For maximum speed, aim for minimal logging with TABLOCK.

MongoDB

# Import CSV / JSON (from the shell)
mongoimport --db=shop --collection=customer --type=csv --headerline --file=customer.csv

# For a JSON array
mongoimport --db=shop --collection=customer --file=customer.json --jsonArray

mongoimport loads CSV/TSV/JSON; --headerline treats the first line as field names. To update existing documents, use --mode=upsert --upsertFields. For large data, --numInsertionWorkers speeds it up.

Redis

# Fastest: feed commands in protocol format and pipe them
cat commands.txt | redis-cli --pipe

# Example lines in commands.txt:
# SET customer:1 "Alice"
# SET customer:2 "Bob"

For bulk loading, use redis-cli --pipe (mass insert); it is very fast because it streams commands in RESP protocol format over a single connection. Alternatively, commands are grouped with pipelining.

Elasticsearch

# _bulk API (NDJSON: an action line + a source line per document)
curl -s -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/customer/_bulk' --data-binary @data.ndjson

# Contents of data.ndjson:
# { "index": {} }
# { "id": 1, "name": "Alice" }
# { "index": {} }
# { "id": 2, "name": "Bob" }

The _bulk API takes NDJSON (an action line + a data line per document; the file must end with \n). For CSV, use Logstash, Filebeat or elasticdump. Disabling refresh_interval during the load speeds it up.

Open in DBAtlas →