How do you perform a 'Bulk Insert' in SQL, and what are its advantages?

Instruction: Explain bulk-loading benefits and tradeoffs. Give a PostgreSQL psql COPY FROM STDIN example and distinguish it from SQL Server BULK INSERT and ordinary SQL statements.

Context: This question probes the candidate's ability to manage large volumes of data efficiently, crucial for performance in bulk data operations.

Updated

Official answer

Bulk loading sends many rows efficiently instead of paying per-row statement and network overhead. The interface is engine-specific: SQL Server has BULK INSERT, while PostgreSQL has COPY. Neither name is universal SQL, and reduced logging is not an unconditional guarantee.

In a fresh PostgreSQL psql session, this complete input loads two CSV rows:

CREATE TABLE import_products (product_id integer PRIMARY KEY, name text NOT NULL);
COPY import_products (product_id, name) FROM STDIN WITH (FORMAT csv);
1,Notebook
2,"Pen, blue"
\.

The last line terminates psql's COPY input; those CSV lines are data, not ordinary SQL statements. A driver uses its COPY streaming API instead. FROM STDIN transfers client data, whereas a server-side file path is interpreted on the database server and needs the relevant privileges.

Validate encoding, quoting, column order, types and rejected rows. Use staging and reconciliation when source quality is uncertain, define transaction size and retry/idempotency behavior, and account for constraints, indexes, WAL, disk space and impact on other workloads. Benchmark representative loads before claiming a speedup.

Related Questions