BEGIN, COMMIT, ROLLBACK in MySQL - Time & Space Complexity
We want to understand how the time taken by transaction commands like BEGIN, COMMIT, and ROLLBACK changes as the amount of work inside the transaction grows.
How does the size of the transaction affect how long these commands take?
Analyze the time complexity of the following code snippet.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
This code starts a transaction, moves money between two accounts, and then saves the changes permanently.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database processes each update statement inside the transaction.
- How many times: Once per update statement; the transaction commands themselves do not loop.
As the number of statements inside the transaction grows, the work done before COMMIT or ROLLBACK grows roughly in direct proportion.
| Input Size (number of statements) | Approx. Operations |
|---|---|
| 10 | About 10 updates processed before commit |
| 100 | About 100 updates processed before commit |
| 1000 | About 1000 updates processed before commit |
Pattern observation: The time grows roughly in a straight line as more statements are added inside the transaction.
Time Complexity: O(n)
This means the time to complete the transaction grows directly with the number of statements inside it.
[X] Wrong: "BEGIN, COMMIT, and ROLLBACK always take the same time no matter how many statements are inside."
[OK] Correct: These commands manage the transaction as a whole, so the more work inside, the longer the commit or rollback takes to finalize all changes.
Understanding how transaction commands scale with workload helps you reason about database performance and reliability in real projects.
"What if we added nested transactions or savepoints? How would the time complexity change?"
Practice
BEGIN statement do in MySQL transactions?Solution
Step 1: Understand the role of BEGIN
TheBEGINstatement marks the start of a transaction block where multiple changes can be grouped.Step 2: Differentiate from COMMIT and ROLLBACK
COMMITsaves changes,ROLLBACKcancels changes, butBEGINonly starts the grouping.Final Answer:
It starts a transaction block where changes can be grouped safely. -> Option BQuick Check:
BEGIN starts transaction [OK]
- Confusing BEGIN with COMMIT
- Thinking BEGIN saves changes
- Assuming BEGIN cancels changes
Solution
Step 1: Recall MySQL transaction start syntax
The simplest and most common syntax in MySQL to start a transaction isBEGIN;.Step 2: Check options for exact syntax
BEGIN TRANSACTION;andOPEN TRANSACTION;are not valid MySQL syntax.Final Answer:
BEGIN; -> Option CQuick Check:
BEGIN; is correct syntax [OK]
- Using BEGIN TRANSACTION; which is invalid in MySQL
- Confusing with other SQL dialects
- Adding extra keywords like OPEN
BEGIN;
INSERT INTO users (name) VALUES ('Alice');
ROLLBACK;
SELECT * FROM users WHERE name = 'Alice';What will the SELECT query return?
Solution
Step 1: Analyze the transaction commands
TheBEGIN;starts a transaction, then an INSERT adds 'Alice'. TheROLLBACK;cancels this change.Step 2: Understand the effect of ROLLBACK
Since the INSERT was rolled back, the row with 'Alice' was never saved.Final Answer:
It will return an empty result (no rows). -> Option AQuick Check:
ROLLBACK cancels changes [OK]
- Assuming INSERT stays after ROLLBACK
- Thinking ROLLBACK saves changes
- Confusing ROLLBACK with COMMIT
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;
ROLLBACK;What is the effect of the final
ROLLBACK;?Solution
Step 1: Follow the transaction flow
BEGIN;starts a transaction, UPDATE changes balance,COMMIT;saves changes permanently.Step 2: Understand ROLLBACK after COMMIT
After commit, no active transaction exists, soROLLBACK;does nothing.Final Answer:
It does nothing because COMMIT already saved changes. -> Option DQuick Check:
ROLLBACK after COMMIT has no effect [OK]
- Thinking ROLLBACK can undo after COMMIT
- Expecting an error on ROLLBACK without active transaction
- Confusing order of COMMIT and ROLLBACK
Solution
Step 1: Identify the need for atomicity
Both updates must succeed or fail together to keep balances consistent.Step 2: Check each option's transaction handling
BEGIN; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT; usesBEGINandCOMMITto group both updates safely. UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; has no transaction, so partial updates can happen. BEGIN; UPDATE accounts SET balance = balance - 200 WHERE id = 1; ROLLBACK; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT; rolls back after first update, so second update is outside transaction. BEGIN; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; ROLLBACK; rolls back after both updates, canceling changes.Final Answer:
BEGIN; UPDATE ...; UPDATE ...; COMMIT; -> Option AQuick Check:
Use BEGIN and COMMIT to group related updates [OK]
- Not using transactions for multi-step updates
- Rolling back too early or too late
- Missing COMMIT to save changes
Ready to prove it?
Pass the test and this pattern moves unknown
