Point-in-time recovery in MySQL - Time & Space Complexity
When restoring a database to a specific moment, the time it takes depends on how much data needs to be replayed.
We want to understand how recovery time grows as the amount of changes increases.
Analyze the time complexity of applying transaction logs for point-in-time recovery.
-- Simplified example of applying logs
START TRANSACTION;
-- Apply each log entry in order
-- For each log entry:
-- UPDATE or INSERT data as recorded
COMMIT;
This snippet represents replaying transaction logs one by one to restore data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying each log entry sequentially.
- How many times: Once for every log entry since the last backup.
As the number of log entries increases, the time to recover grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 log entries applied |
| 100 | 100 log entries applied |
| 1000 | 1000 log entries applied |
Pattern observation: Doubling the number of log entries roughly doubles the recovery time.
Time Complexity: O(n)
This means recovery time grows directly with the number of log entries to apply.
[X] Wrong: "Recovery time stays the same no matter how many changes happened."
[OK] Correct: More changes mean more logs to apply, so recovery takes longer.
Understanding how recovery time scales helps you design better backup and restore strategies, a useful skill in real database work.
"What if we compressed or grouped log entries before applying them? How would the time complexity change?"