MySQL Interview Questions Explained: Indexes, Clustered Storage, and Query Cost

    A practical walkthrough of the MySQL questions that come up most often in interviews, following a single reasoning chain from B+ tree index structure to clustered index storage, covering indexes versus table lookups, common reasons indexes are not used, and how to verify findings with execution plans and monitoring data. Readers get the structural reasoning behind each conclusion, the tradeoffs behind primary key and composite index design, and a four-layer framework for answering and applying these ideas in real work.

    Why These Questions Are Worth Revisiting

    In day-to-day development, the database is often the component developers can use but cannot explain. CRUD operations have almost no learning curve, yet as soon as the topic turns to index design, slow query diagnosis, or the boundaries of locks and transactions, many developers stay at the level of personal experience. Interviews favor these questions not to test memorization, but to confirm whether a candidate truly understands how data is organized on disk, what stages a query passes through, and where the cost is actually spent.

    The questions below form a complete reasoning chain: underlying data structure → how data and indexes are stored → covering indexes versus table lookups → index failure and optimization. Each step builds on the one before it, and if the chain breaks at the very first link, the follow-up questions cannot really unfold.

    The Underlying Index Structure: Why a B+ Tree

    An index is essentially a trade of extra space for a shorter search path. When evaluating whether a data structure suits a disk-based index, two things matter most: whether the tree height is small enough, and whether efficient range access is supported.

    A B+ tree is a multi-way balanced search tree with the following structural characteristics:

    • Non-leaf nodes store only keys and pointers to child nodes, not the actual data;
    • All data rows live in the leaf nodes;
    • Leaf nodes are linked through a doubly linked list, forming an ordered sequence.

    The benefits of these three points stack. Because non-leaf nodes hold no data, a single page (commonly 16KB) can fit hundreds or thousands of keys and pointers, which increases fan-out and keeps tree height down to typically three or four levels. A short tree means a short search path, and each level descended roughly corresponds to one disk I/O, so the number of I/Os for a point lookup has an upper bound. The ordered linked list in the leaf nodes lets range queries, sorting, and pagination be completed by sequential scans, without repeatedly going back up to the higher levels.

    By comparison, a balanced binary tree (such as a red-black tree) grows in height too quickly as data volume increases; in a disk scenario every comparison may cost one I/O, which is unacceptable. A multi-way balanced tree that stores data in every node has smaller fan-out and greater height, so it is no better. The B+ tree strikes the best engineering balance between “low tree height” and “range friendly,” which is why mainstream relational databases generally choose it.

    A simplified cost model for a single point lookup:

    1. Search path length ≈ log_fanout(N)
    2. N = 20 million, fan-out ≈ 1000 → path length ≈ 3

    Note that these numbers are an order-of-magnitude illustration derived from the structure. Actual behavior depends on page fill rate, key length, and cache hit rate, so they should not be treated as fixed conclusions.

    Where the Data Actually Lives: Clustered Index and the Hidden Primary Key

    The second frequent question concerns the relationship between indexes and data. InnoDB-style engines in mainstream relational databases organize a table with a clustered index: the leaf nodes of the primary key index hold the entire row directly, so the table itself is one B+ tree. There are not two separate structures, a data file plus an independent index file.

    When no primary key exists, the fallback order is usually:

    1. Use the primary key explicitly defined by the user, if present;
    2. Otherwise, pick the first unique index whose columns are all non-null;
    3. If none exists, the engine generates a fixed-length hidden row identifier as the clustered key.

    This order explains a common practical recommendation: define an explicit primary key for the table, and prefer values that are monotonically increasing and short. The reason is that the clustered index key appears in the leaf nodes of every secondary index, so a longer primary key makes secondary indexes larger; an unordered primary key causes page splits and fragmentation, with obvious write amplification.

    Covering Indexes and Table Lookups

    When all the columns a query needs can be obtained directly from an index, the engine does not have to go back to the clustered index for the full row. This is a covering index. Its value is not only saving one lookup, but turning random access into a sequential scan over the index, which is a friendlier I/O pattern.

    Conversely, if the index is missing a required column, a table lookup happens: the secondary index is used to find the primary key, then the primary key is used to fetch the full row from the clustered index. When table lookups are numerous and randomly distributed, the cost escalates quickly.

    1. -- Assume an index idx_status_created(status, created_at)
    2. -- Covering index: all required columns are in the index
    3. SELECT status, created_at FROM orders WHERE status = 1;
    4. -- Requires a table lookup: order_no is not in the index
    5. SELECT order_no FROM orders WHERE status = 1;

    A common optimization is to organize frequently queried columns in a composite index with equality conditions first and range or sort columns after, so as many queries as possible take the covering path. Whether it is worth widening an index for coverage depends on a tradeoff against write pressure and space cost.

    Typical Scenarios Where an Index Is Not Used

    Interviews often follow up with “why is it still slow with an index.” The frequent causes come down to:

    • Applying a function or expression to an indexed column;
    • Implicit type conversion, such as comparing a string column to a number;
    • A composite index that does not satisfy the leftmost prefix rule;
    • A leading wildcard match;
    • The optimizer deciding, based on statistics, that a full table scan is cheaper;
    • Too many table lookups, where random I/O cancels out the index benefit.

    Diagnosis should be evidence-driven rather than guesswork: first use the execution plan to confirm the actual access path and estimated row count, then cross-check against the slow query log, statistics freshness, cache hit rate, and monitoring metrics. If the deployment is on a cloud platform, the monitoring, alerting, and log services offered by the cloud provider can help establish a baseline, but the conclusion should still come from the real data of the instance in question.

    A Framework for Answering and Applying This

    For questions like these, answer in four layers: structure → organization → cost → verification. Start with the underlying data structure and its design tradeoffs, then explain how data and indexes are stored, then analyze the I/O and table lookup cost of a query, and finally show how to verify with execution plans and monitoring data. The same framework applies to real work: consider the primary key and composite indexes during table design, recheck with execution plans before release, and keep calibrating with slow queries and monitoring after release.

    What really sets people apart is not how many conclusions they have memorized, but whether they can explain the premises and boundaries under which each conclusion holds.