Databases 7 min read

Understanding MySQL Locks: Table, Row, and Page Locking Mechanisms

This article explains why MySQL uses locks, describes table‑level, row‑level, and page‑level locking, outlines lock types such as shared, exclusive, and intention locks, and provides practical usage examples and common troubleshooting tips for InnoDB and other storage engines.

Top Architect
Top Architect
Top Architect
Understanding MySQL Locks: Table, Row, and Page Locking Mechanisms

1. Why Does a Database Need Locks?

Locks ensure data consistency; MySQL supports table‑level, row‑level, and page‑level locking across its various storage engines.

2. Table‑Level Locks

Characteristics

• Large granularity, high lock‑conflict probability, low concurrency. • Advantages: no deadlocks, low overhead, fast acquisition/release. • Used by non‑transactional engines such as MyISAM, MEMORY, CSV, suitable for read‑heavy, write‑light workloads.

3. Row‑Level Locks

Advantages & Disadvantages

• Fine granularity, low conflict probability, high concurrency. • Drawbacks: higher overhead, slower acquisition, can cause deadlocks.

InnoDB Row‑Lock Types

• Shared lock (S) : multiple transactions can read the same row but cannot modify it. • Exclusive lock (X) : only the locking transaction can read and modify the row. • Intention locks : automatically added by InnoDB; they do not conflict with row‑level shared or exclusive locks.

Usage Scenarios

• Shared lock: verify a row’s existence while preventing updates/deletes. • Exclusive lock: needed when a transaction intends to update a row.

SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE;
SELECT * FROM table_name WHERE ... FOR UPDATE;

Important Notes

• InnoDB acquires exclusive locks automatically for UPDATE, DELETE, INSERT statements. • SELECT statements without FOR UPDATE/LOCK IN SHARE MODE do not acquire row locks.

4. Page‑Level Locks

Page locks sit between row and table locks, can cause deadlocks, and are used by engines like BDB.

5. Common Lock‑Related Questions

When does InnoDB lock an entire table vs. a single row?

If a query lacks an index, InnoDB falls back to a table lock; otherwise it uses row locks.

When to use row‑level vs. table‑level locks?

Use row‑level locks for indexed queries requiring high concurrency; use table‑level locks for non‑indexed operations.

What does a row lock actually lock?

Row locks are applied on index entries, not directly on the data rows.

Read vs. write locks in MySQL

SELECT triggers a read lock; UPDATE/DELETE/INSERT trigger a write (exclusive) lock, released on COMMIT or ROLLBACK.

Lock algorithms

• Next‑Key lock = Gap lock + Record lock. • Gap lock locks only the gap before a record. • Record lock locks the record itself.

When are locks released?

Locks are released when the transaction commits or rolls back.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLdatabaseconcurrencyInnoDBmysqlLocks
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.