Transaction states in DBMS Theory - Time & Space Complexity
When working with transactions in a database, it is important to understand how the time taken changes as the transaction moves through different states.
We want to know how the processing time grows as the transaction progresses from start to finish.
Analyze the time complexity of the following transaction state changes.
BEGIN TRANSACTION;
-- Perform several SQL operations
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
This snippet shows a simple transaction that updates two accounts and then commits the changes.
Look for repeated steps or operations that affect time.
- Primary operation: Executing SQL update statements inside the transaction.
- How many times: Each update runs once, but the number of operations depends on how many rows are affected.
The time taken depends on how many rows each update affects and how complex the operations are.
| Input Size (rows affected) | Approx. Operations |
|---|---|
| 10 | About 10 updates per statement |
| 100 | About 100 updates per statement |
| 1000 | About 1000 updates per statement |
Pattern observation: As the number of rows affected grows, the time grows roughly in direct proportion.
Time Complexity: O(n)
This means the time to complete the transaction grows linearly with the number of rows affected by the operations inside it.
[X] Wrong: "The transaction time is always constant regardless of data size."
[OK] Correct: The time depends on how many rows the transaction modifies; more rows mean more work and longer time.
Understanding how transaction time grows helps you explain database performance and reliability in real projects.
"What if the transaction included a loop that updated rows one by one instead of a single update statement? How would the time complexity change?"
Practice
Solution
Step 1: List common transaction states
Common transaction states include Running (active), Waiting (blocked), Committed (successfully completed), and Aborted (rolled back).Step 2: Identify invalid state
"Failed" is not a standard transaction state; instead, "Aborted" or "Rolled back" is used for unsuccessful transactions.Final Answer:
Failed -> Option DQuick Check:
Standard states exclude "Failed" [OK]
- Confusing 'Failed' with 'Aborted'
- Thinking 'Waiting' is not a state
- Assuming 'Failed' is official terminology
Solution
Step 1: Understand transaction commands
ROLLBACK undoes changes, SAVEPOINT sets a restore point, BEGIN starts a transaction, and COMMIT finalizes changes.Step 2: Identify command to commit
COMMIT is the command that moves a transaction from Running (active) to Committed (completed) state.Final Answer:
COMMIT -> Option BQuick Check:
COMMIT finalizes transaction [OK]
- Confusing ROLLBACK with COMMIT
- Using SAVEPOINT to commit
- Thinking BEGIN commits
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
What is the final state of this transaction after execution?
Solution
Step 1: Analyze the transaction commands
The transaction starts with BEGIN, performs two updates, then ends with COMMIT.Step 2: Determine transaction state after COMMIT
COMMIT finalizes the transaction successfully, so the state becomes Committed.Final Answer:
Committed -> Option AQuick Check:
COMMIT means transaction is Committed [OK]
- Thinking transaction remains Running after COMMIT
- Confusing Aborted with Committed
- Assuming Waiting state after COMMIT
BEGIN;
UPDATE inventory SET quantity = quantity - 5 WHERE product_id = 10;
ROLLBACK;
COMMIT;
Solution
Step 1: Understand ROLLBACK and COMMIT order
ROLLBACK cancels the transaction, so no changes remain to commit afterwards.Step 2: Identify invalid command sequence
After ROLLBACK, issuing COMMIT is invalid because the transaction is already aborted.Final Answer:
COMMIT after ROLLBACK is invalid -> Option AQuick Check:
Cannot COMMIT after ROLLBACK [OK]
- Thinking COMMIT can follow ROLLBACK
- Assuming ROLLBACK should come after COMMIT
- Missing BEGIN statement
Solution
Step 1: Understand 'Waiting' state cause
Waiting occurs when a transaction is blocked due to locks held by another transaction.Step 2: Analyze actions that unblock waiting
If the blocking transaction commits, rolls back, or releases the lock, the waiting transaction can proceed.Step 3: Identify action that does NOT help
The waiting transaction issuing COMMIT while blocked cannot proceed because it must first acquire the lock.Final Answer:
The waiting transaction issues a COMMIT -> Option CQuick Check:
Waiting means blocked; only blocking transaction actions help [OK]
- Thinking waiting transaction can commit while blocked
- Assuming waiting state ends without lock release
- Confusing roles of blocking and waiting transactions
Ready to prove it?
Pass the test and this pattern moves unknown
