Databases 17 min read

10 Core MySQL InnoDB Interview Questions Explained

This article walks through ten essential MySQL InnoDB interview questions, covering why InnoDB uses B+ trees, MVCC isolation levels, two‑phase commit, gap locks, buffer‑pool LRU design, undo‑log mechanics, binlog formats, lock‑upgrade behavior, double‑write buffering, and the differences between clustered and non‑clustered indexes.

Programmer1970
Programmer1970
Programmer1970
10 Core MySQL InnoDB Interview Questions Explained

1. Why does InnoDB choose B+Tree over BTree for indexes?

Answer:

Disk I/O optimization : Non‑leaf nodes store only keys, allowing more keys per 16KB page (typically 1000+), reducing node count.

Range query optimization : Leaf nodes form a doubly linked list, enabling range scans in O(logN + k) time.

Storage capacity : A three‑level B+Tree can hold ~20 million rows (1000³), while a BTree would need more levels.

Bonus: In write‑intensive workloads, LSM trees can outperform B+Trees because they write sequentially and avoid page splits.

2. How does InnoDB implement MVCC for different isolation levels?

Answer:

Read View : Created at transaction start, containing active transaction IDs; a transaction sees only data committed before the view.

Isolation level behavior :

Read Uncommitted – no MVCC, reads latest data (dirty reads).

Read Committed – a new Read View per query, sees committed changes (no repeatable reads).

Repeatable Read – a single Read View for the whole transaction, preventing non‑repeatable reads.

Serializable – no MVCC, uses locking to enforce serial execution.

Key details : Each clustered row stores hidden fields DB_TRX_ID (last modifying transaction) and DB_ROLL_PTR (pointer to its undo log). DB_ROLL_PTR retrieves previous versions for MVCC.

Bonus: Under Repeatable Read, phantom reads can still occur; InnoDB mitigates this with Next‑Key locking.

3. Why does InnoDB use a two‑phase commit (2PC) and how does it work?

Answer:

Commit phase :

Write transaction to binlog buffer and flush to disk.

Mark transaction state as "commit".

Write redo log to redo‑log buffer and flush.

Mark state as "prepare" and write to binlog.

Why 2PC?

Ensures binlog and redo log stay consistent; missing either would cause data loss in replication or crash recovery.

Crash recovery :

If a transaction is in "prepare" and binlog was written, it is committed.

If binlog was not written, the transaction is rolled back.

Bonus: 2PC is the foundation for MySQL replication and durability.

4. How does InnoDB's gap lock work and why does it prevent phantom reads?

Answer:

Definition : Gap locks lock the space between index records, not the records themselves (e.g., gaps between values 1‑5 and 5‑10).

Phantom‑read solution : In Repeatable Read, InnoDB adds gap locks to range predicates, e.g., SELECT * FROM users WHERE age BETWEEN 20 AND 30 locks the (20,30) gap, blocking other transactions from inserting rows in that range.

Next‑Key lock : Combination of row lock + gap lock, e.g., locking (20,30] includes both rows and the gap.

Why not all range queries get gap locks? Unique index equality queries use only row locks; non‑unique indexes use Next‑Key locks.

Bonus: In high‑concurrency scenarios, gap locks increase lock contention; using Read Committed with optimistic versioning can reduce overhead.

5. How does the InnoDB Buffer Pool work and why is a specialized LRU needed?

Answer:

Purpose : Caches disk pages to reduce I/O and uses pre‑read to load likely needed pages.

LRU implementation : Split into a young list and an old list . New pages enter the old list; after a default 1 second they move to the young list, preventing full‑table scans from filling the pool with cold pages.

Key parameters : innodb_old_blocks_time – minimum time (default 1000 ms) a page stays in the old list. innodb_old_blocks_pct – percentage of the LRU occupied by the old list (default 37%).

Bonus: Pre‑warming the pool at startup with innodb_buffer_pool_load_at_startup avoids cold‑start performance penalties.

6. How does InnoDB's Undo Log enable transaction rollback and MVCC?

Answer:

Undo Log purpose : Provides atomicity (rollback) and supports MVCC (consistency).

Storage structure : Each entry records transaction ID, pointer to old data, and the SQL needed to reverse the change. Undo logs reside in the Undo tablespace (system or dedicated). Each transaction's undo records are linked in a list.

Rollback process : Use DB_ROLL_PTR to locate the old version, then execute the inverse SQL (e.g., DELETE for an UPDATE).

MVCC usage : Read View accesses the undo log via DB_ROLL_PTR to reconstruct historical row versions.

Cleanup : The purge thread removes undo records once all active transactions have committed and no longer need the old versions.

Bonus: Undo Log is logical (stores how to undo) whereas Redo Log is physical (stores how to redo).

7. What are the pros and cons of the three MySQL binlog formats?

Answer:

Statement :

Logs the original SQL.

Pros: Small log size.

Cons: Non‑deterministic functions (NOW(), RAND()) can cause master‑slave divergence.

Best for simple SELECT/UPDATE/DELETE without nondeterministic functions.

Row :

Logs each changed row.

Pros: High data consistency, no nondeterministic issues.

Cons: Large log size, especially for bulk updates.

Best for scenarios demanding strong consistency, e.g., financial systems.

Mixed :

Defaults to Statement; switches to Row for nondeterministic statements.

Pros: Balances size and consistency.

Cons: Switching logic may not be perfect.

Best for general use when statement types are uncertain.

Bonus: MySQL 8.0 recommends Row format for its consistency guarantees.

8. Why does InnoDB upgrade row locks to a table lock after >5000 rows?

Answer:

Mechanism : When the number of row locks exceeds 5000, InnoDB automatically upgrades to a table lock. The threshold can be changed via innodb_row_locks.

Reason : Reduces lock‑management overhead and improves overall concurrency.

Impact : The table becomes inaccessible to other transactions, which may degrade performance, but overall overhead is lower than managing thousands of row locks.

Avoiding upgrade : Optimize queries to lock fewer rows, split large transactions, or use a lower isolation level such as Read Committed.

Bonus: Frequent upgrades in high‑concurrency workloads may indicate a need to tune isolation levels or query patterns.

9. What is the Double Write Buffer and why is it needed?

Answer:

Purpose : Guarantees page consistency during crash recovery and prevents partial page writes.

How it works :

Data pages are first written to the Double Write Buffer (a region in the shared tablespace).

Then the pages are flushed to their final locations on disk.

If a crash occurs mid‑write, InnoDB can recover the page from the buffer.

Why needed : Disk writes are atomic at sector level (≈512 B) but MySQL pages are 16 KB; a crash during a page write could corrupt part of the page. Double write ensures full‑page integrity.

Bonus: Together with Redo Log, it forms the core of MySQL’s durability mechanism.

10. Differences between clustered and non‑clustered indexes and why the primary key is clustered

Answer:

Clustered index :

Data rows are stored in the leaf nodes of the index; the index is the table.

Only one per table, usually the primary key.

Primary‑key lookups require a single I/O.

Non‑clustered index :

Index and data are stored separately.

Leaf nodes contain only the primary‑key value.

Queries need a “back‑lookup” (fetch primary key from index, then fetch the row).

Why primary key is clustered : Leaf nodes hold the full row, making primary‑key reads fastest; non‑clustered indexes incur extra I/O.

Optimizations :

Covering indexes avoid back‑lookups by containing all needed columns.

Index push‑down (supported from InnoDB 5.6) pushes predicates to the index layer.

Bonus: Choosing an auto‑increment integer primary key reduces page splits; using UUIDs can cause frequent splits and degrade performance.

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.

InnoDBMySQL2PCB+TreeMVCCGap LockBuffer PoolBinlog Formats
Programmer1970
Written by

Programmer1970

Formerly called 'Code to 35'. Add our main WeChat ID to access a wealth of shared resources (algorithms, interview prep, tech stacks: Java, Python, Go, big data). We mainly share serious development techniques, focusing on output-driven input. Occasionally we post life snippets and gossip. Our aim is to attract precise traffic and test advertising opportunities.

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.