Database Deadlock Prevention Strategies | Generated by AI

Home 2026.04

Question: How does deadlock happen in a database like MySQL or DB2, and is it better to execute SQL statements one by one or batch them into a transaction to reduce deadlock probability?

Answer:

1. How deadlocks happen in databases

A deadlock occurs when two (or more) transactions are waiting on each other’s locks in a cycle.

Classic example:

Now:

Deadlock formed

Both MySQL (InnoDB) and IBM Db2 detect this and kill one transaction to break the cycle.


2. What actually causes deadlocks (important mental model)

Deadlocks are not about time alone, but about:

Key factors:

Factor Effect
Different access order 🚨 Main cause
Long transaction duration Increases probability
High concurrency Increases contention
Range locks / index gaps Makes it worse

3. Your idea: “execute SQL one by one vs batch in transaction”

You are thinking:

many small SQL calls → lock/unlock repeatedly → more contention batch SQLs → shorter total lock time → better

This is partially correct but incomplete.

Let’s analyze both:


Case A: Execute SQL one-by-one (auto-commit mode)

Each SQL:

Pros:

Cons:


Case B: Batch SQLs in a transaction

BEGIN;
UPDATE ...;
UPDATE ...;
COMMIT;

Pros:

Cons:


4. Your key assumption: “200ms vs 20ms lock time”

This is where you need correction.

👉 Deadlocks are NOT proportional to total lock time alone

Even a 20ms transaction can deadlock if:

And a 200ms transaction may NOT deadlock if:


5. What actually reduces deadlocks (important)

✅ 1. Consistent lock ordering (MOST IMPORTANT)

Always access rows/tables in the same order:

-- Always do id=1 then id=2 (never reverse)

This alone eliminates most deadlocks.


✅ 2. Keep transactions short (but not fragmented)


✅ 3. Proper indexing

Without index:

UPDATE table WHERE non_index_column = ...

→ may lock many rows or even full table


✅ 4. Avoid unnecessary locks


✅ 5. Retry logic

Deadlocks are normal in high concurrency systems

Both MySQL and DB2 expect:

try:
    run transaction
catch deadlock:
    retry

6. Final answer to your question

Is batching SQLs into a transaction better than executing one-by-one?

👉 Answer: It depends, but generally YES—with conditions


7. Correct mental model (important takeaway)

Instead of thinking:

“reduce total lock time from 200ms → 20ms”

Think:

“ensure all transactions acquire locks in the SAME ORDER and finish QUICKLY”

That’s what really prevents deadlocks.


8. Simple intuition


If you want, I can show you real InnoDB deadlock logs and how to analyze them line-by-line—that’s where understanding becomes very concrete.


Back Donate