Deadlock handling in databases in DBMS Theory - Time & Space Complexity
When databases handle deadlocks, they must detect and resolve conflicts between transactions.
We want to understand how the time to handle deadlocks grows as the number of transactions increases.
Analyze the time complexity of the following deadlock detection process.
-- Assume transactions and their locks are stored in a wait-for graph
FOR each transaction T1 IN transactions LOOP
FOR each transaction T2 WAITED BY T1 LOOP
IF T2 waits for T1 THEN
-- Deadlock detected
RESOLVE deadlock;
END IF;
END LOOP;
END LOOP;
This code checks pairs of transactions to find cycles indicating deadlocks.
Look for loops or repeated checks in the code.
- Primary operation: Nested loops checking transaction pairs in the wait-for graph.
- How many times: For each transaction, it checks all transactions it waits for, potentially all others.
As the number of transactions grows, the checks increase quickly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks |
| 100 | About 10,000 checks |
| 1000 | About 1,000,000 checks |
Pattern observation: The number of checks grows roughly with the square of the number of transactions.
Time Complexity: O(n2)
This means if the number of transactions doubles, the time to detect deadlocks roughly quadruples.
[X] Wrong: "Deadlock detection time grows linearly with the number of transactions."
[OK] Correct: Because each transaction may wait for many others, the checks multiply, causing quadratic growth, not just a simple line.
Understanding how deadlock detection scales helps you design better database systems and shows you can think about system performance clearly.
"What if the system used a different data structure that tracks waiting transactions more efficiently? How would the time complexity change?"