INSERT Patterns: Performance
Module: Data Modification & Transactions
INSERT performance optimization: (1) Bulk inserts: 10-100x faster than single inserts (1 transaction vs N transactions). (2) INSERT...SELECT: 100x faster than row-by-row (server-side operation, no network overhead). (3) COPY/LOAD DATA: 100x faster than INSERT (bypasses SQL parsing, direct data load). (4) Disable indexes: 10x faster for large loads (rebuild indexes once after load). (5) Batch commits: 100x faster (commit every 1000 rows, not every row). (6) Disable triggers: Faster for bulk loads (re-enable after load). (7) Batch size: 1000-5000 rows optimal (balance performance and memory). Real-world: Shopify uses bulk inserts for flash sales (50K orders/min). Amazon uses COPY for data warehouse (billions of rows). Netflix batches log inserts (1000 rows every 5 seconds).
Bulk INSERT: 10-100x faster than single inserts (1 transaction vs N transactions, 1 network round-trip vs N)
INSERT...SELECT: 100x faster than row-by-row (server-side operation, no network overhead)
COPY/LOAD DATA: 100x faster than INSERT (bypasses SQL parsing, direct data load)
Disable indexes: 10x faster for large loads (rebuild indexes once after load instead of updating per row)
Batch commits: Commit every 1000-5000 rows (balance performance and memory, avoid huge transactions)
Disable triggers: Faster for bulk loads (re-enable after load, avoid trigger overhead per row)
Optimal batch size: 1000-5000 rows per batch (too small = too many commits, too large = too much memory)
Use UNLOGGED tables: PostgreSQL UNLOGGED tables skip WAL (10x faster writes, but no crash recovery)
Single INSERT in loop: Very slow (N transactions, N network round-trips), use bulk INSERT instead
Not specifying column list: INSERT VALUES without columns breaks if table schema changes
Not using RETURNING/OUTPUT: Extra SELECT query needed to get IDs (slower, race conditions possible)
Large transactions: Inserting millions of rows in 1 transaction uses too much memory, batch commits instead
Not disabling indexes: Large loads are 10x slower with indexes enabled, disable and rebuild after