Batching & Transactions in SQL
Fewer round-trips, grouped writes, all-or-nothing units
Two workhorses of real-world performance. Batching: one INSERT of 500 rows beats 500 single INSERTs, because the engine parses and commits once instead of per row. Transactions: wrapping related writes in BEGIN … COMMIT makes them one all-or-nothing unit — either every change lands or none does, and readers never see the half-done middle.
One statement, many rows
INSERT INTO t (…) VALUES (…), (…), (…) — a single parse, plan and commit for hundreds of rows instead of one round-trip each.
Group writes into transactions
BEGIN; … COMMIT; makes a set of statements one unit of work — all visible together, or not at all.
Commit once, fsync once
The durability flush (the expensive part) happens per COMMIT — a thousand writes in one transaction share a single flush.
ROLLBACK as the safety net
If statement 3 of 4 fails, ROLLBACK returns the database to the pre-BEGIN state — no half-done work is ever visible.
Keep them short
Long transactions hold locks and bloat the log. Batch the rows inside the transaction; never wrap hours of user think-time in one.
1BEGIN;2UPDATE employees SET salary = salary * 1.05 WHERE department = 'Engineering';3INSERT INTO audit (action, done_at) VALUES ('raise_engineering', NOW());4COMMIT;
| result |
|---|
| 3 rows updated, 1 row inserted — transaction committed |
The raise and its audit row are one unit: if the INSERT fails, ROLLBACK removes the raise too — an audit log that never lies. Committing once also means one durability flush instead of two, and other sessions see either “no raise, no audit row” or both, never a half-finished state.
Multi-row INSERT
The simplest batching win.
1INSERT INTO employees (id, name, department, salary, age) VALUES2 (8, 'Hana', 'Sales', 54000, 28),3 (9, 'Ivan', 'Engineering', 77000, 36),4 (10, 'Jules', 'Marketing', 51000, 25);
| result |
|---|
| 3 rows inserted in one statement — one parse, one plan, one commit |
All-or-nothing business operation
The raise and its audit row live or die together.
1BEGIN;2UPDATE employees SET salary = salary * 1.05 WHERE department = 'Engineering';3INSERT INTO audit (action, done_at) VALUES ('raise_engineering', NOW());4COMMIT;
| result |
|---|
| 3 rows updated, 1 row inserted — transaction committed |
ROLLBACK — the undo button
Mistakes inside a transaction vanish.
1BEGIN;2DELETE FROM employees;3-- forgot the WHERE. panic? no —4ROLLBACK;5SELECT COUNT(*) FROM employees;
| count |
|---|
| 6 |
What others see mid-transaction
Isolation keeps half-done work invisible.
1BEGIN;2UPDATE employees SET salary = 56000 WHERE id = 2;3-- second session: SELECT salary FROM employees WHERE id = 2;4-- → 52000 (last committed)5COMMIT;6-- second session now reads 56000
| observer | seen value |
|---|---|
| before commit: 52000 | after commit: 56000 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.