Deadlock concept and prevention in SQL - Time & Space Complexity
Deadlocks happen when two or more database operations wait for each other to finish, causing a standstill.
We want to understand how the waiting time grows as more transactions compete for resources.
Analyze the time complexity of this simple deadlock-prone transaction sequence.
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 200 WHERE id = 2;
UPDATE accounts SET balance = balance + 200 WHERE id = 1;
COMMIT;
This code shows two transactions updating the same rows but in reverse order, which can cause a deadlock.
Look for operations that wait or retry due to deadlock.
- Primary operation: Lock acquisition on rows during UPDATE statements.
- How many times: Each transaction tries to lock two rows; if deadlock occurs, one retries.
As more transactions try to update overlapping rows in conflicting order, waiting and retries increase.
| Input Size (n transactions) | Approx. Operations (lock attempts + retries) |
|---|---|
| 2 | Few retries if deadlock occurs |
| 10 | More retries as conflicts rise |
| 100 | Many retries, longer waits, possible cascading delays |
Pattern observation: Waiting and retries grow quickly as more transactions compete for the same resources.
Time Complexity: O(n)
This means the waiting and retry time grows roughly in proportion to the number of competing transactions.
[X] Wrong: "Deadlocks only happen rarely and don't affect performance much."
[OK] Correct: Even a few deadlocks cause retries and waiting, which add up as more transactions run concurrently.
Understanding deadlocks helps you design safer database operations and shows you can think about how systems behave under load.
"What if transactions always locked rows in the same order? How would the time complexity change?"
Practice
Solution
Step 1: Understand transaction locking
Transactions lock resources to maintain data integrity during operations.Step 2: Identify deadlock condition
Deadlock occurs when transactions wait on each other's locked resources indefinitely, causing a standstill.Final Answer:
A situation where two or more transactions wait indefinitely for each other to release locks -> Option DQuick Check:
Deadlock = Transactions waiting forever [OK]
- Confusing deadlock with syntax errors
- Thinking deadlock speeds up queries
- Assuming deadlock is a backup process
Solution
Step 1: Understand deadlock prevention strategy
Accessing resources in a consistent order prevents circular waiting among transactions.Step 2: Evaluate options
Only accessing tables in the same order avoids deadlocks; other options cause errors or do not help.Final Answer:
Always access tables in the same order in all transactions -> Option CQuick Check:
Consistent access order = Deadlock prevention [OK]
- Ignoring transaction commit timing
- Using SELECT * without filters causes performance issues, not deadlocks
- Dropping tables is unrelated to deadlock prevention
Transaction 1: LOCK table A; LOCK table B;Transaction 2: LOCK table B; LOCK table A;What is the likely outcome?
Solution
Step 1: Analyze lock order in transactions
Transaction 1 locks table A then B; Transaction 2 locks table B then A, creating circular wait.Step 2: Identify deadlock condition
Each transaction waits for the other's locked resource, causing deadlock.Final Answer:
Deadlock occurs because each waits for the other's locked table -> Option AQuick Check:
Opposite lock order = Deadlock [OK]
- Assuming transactions run without waiting
- Confusing deadlock with syntax errors
- Thinking one transaction commits immediately
Solution
Step 1: Identify cause of deadlock
Deadlocks often happen due to inconsistent lock order in transactions.Step 2: Apply fix by ordering locks
Changing transactions to lock tables in the same order prevents circular waiting and deadlocks.Final Answer:
Change transactions to lock tables in the same order -> Option BQuick Check:
Consistent lock order fixes deadlocks [OK]
- Removing COMMIT causes longer locks, worsening deadlocks
- SELECT without WHERE does not prevent locks
- Higher isolation can increase deadlocks
Orders and Customers. Which approach is best?Solution
Step 1: Understand deadlock prevention principles
Accessing resources in the same order and keeping transactions short reduces deadlocks.Step 2: Evaluate options for best practice
Ensure all transactions updateCustomersfirst, thenOrders, and keep transactions short enforces consistent order and short transactions, preventing deadlocks effectively.Step 3: Analyze other options
Allow transactions to update tables in any order but increase isolation level to SERIALIZABLE may increase deadlocks; Use long transactions with many updates to reduce lock frequency lengthens locks; Disable locking to speed up updates is unsafe and invalid.Final Answer:
Ensure all transactions updateCustomersfirst, thenOrders, and keep transactions short -> Option AQuick Check:
Consistent order + short transactions = Deadlock prevention [OK]
- Increasing isolation level can cause more deadlocks
- Long transactions hold locks longer, increasing deadlock risk
- Disabling locking breaks data integrity
Ready to prove it?
Pass the test and this pattern moves unknown
