In MySQL with InnoDB, which kinds of index can you create, and which data structure backs each kind?
answer
- five keywords in CREATE INDEX
- one structure serves most of them
- words get their own index
- geometry gets a tree of boxes
basics
~20 sInnoDB builds PRIMARY KEY, UNIQUE and plain indexes as B-trees, the primary key being the clustered index that holds the rows. FULLTEXT indexes are inverted word lists, SPATIAL indexes are R-trees, and a requested HASH index becomes a B-tree.
solid answer
~40 sOn an InnoDB table the `PRIMARY KEY` is the clustered index: a B-tree whose leaves are the rows themselves. `UNIQUE` and plain `INDEX`/`KEY` indexes are secondary B-trees, and `BTREE` is the only `index_type` InnoDB permits, so `USING HASH` quietly produces a B-tree (hash indexes belong to the `MEMORY` engine). `FULLTEXT` indexes are inverted indexes, a word-to-rows list queried with `MATCH() ... AGAINST()`, never with `LIKE`. `SPATIAL` indexes are R-trees over `NOT NULL` geometry columns, and the optimizer only uses them when the column carries an `SRID` attribute. InnoDB also builds multi-valued indexes over JSON arrays with `CAST(... AS ... ARRAY)`. The adaptive hash index is an internal cache, not something you create, and MySQL 8.4 turned it off by default.
code
sql · 14 linesCREATE TABLE stores (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
code VARCHAR(20) NOT NULL,
city VARCHAR(100) NOT NULL,
description TEXT,
location POINT NOT NULL SRID 4326,
PRIMARY KEY (id),
UNIQUE KEY uk_code (code),
KEY idx_city (city),
FULLTEXT KEY ft_description (description),
SPATIAL KEY sp_location (location)
) ENGINE=InnoDB;
SHOW INDEX FROM stores;go deeper
Recall the five keywords and that InnoDB builds B-trees for primary, unique and plain indexes, an inverted index for FULLTEXT and an R-tree for SPATIAL.
Explain why the primary key is the clustered index, why USING HASH silently becomes BTREE, and how MATCH() AGAINST() differs from LIKE.
Show you know the traps that waste an index: an SRID-less spatial column, a minimum token size that drops short codes, a MATCH() column list that fits no index.
Frame index kinds as product choices: when built-in full-text search is enough for a feature and when its ranking and tokenising limits justify a dedicated search system.
## The index kinds InnoDB offers MySQL's `CREATE INDEX` grammar names the kinds directly: `CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX`, plus the `PRIMARY KEY` clause that `CREATE TABLE` or `ALTER TABLE` declares. Which structure you get depends on the storage engine, and for InnoDB, the default engine, the answer is short. | Kind | Declared with | Structure in InnoDB | Typical use | |---|---|---|---| | Primary key | `PRIMARY KEY (...)` | B-tree, clustered (leaves hold the rows) | row identity, lookups by id | | Unique | `UNIQUE KEY` | secondary B-tree | enforcing a business key such as an email | | Plain | `INDEX` / `KEY` | secondary B-tree | equality, range and sorted access | | Full-text | `FULLTEXT KEY` | inverted index | word search in `CHAR`, `VARCHAR`, `TEXT` | | Spatial | `SPATIAL KEY` | R-tree | bounding-box searches on geometry | | Multi-valued | `INDEX ((CAST(col->'$.path' AS ... ARRAY)))` | secondary B-tree with several entries per row | membership tests on JSON arrays | ## B-tree indexes: primary, unique and plain - The **primary key** is the **clustered index**. Its leaf pages are the table's rows, so looking a row up by primary key ends on the data itself. A table without one gets a clustered index from its first `UNIQUE` index on `NOT NULL` columns, or else from a hidden row id. - **Secondary indexes** (`UNIQUE` and plain) are separate B-trees whose entries end with the row's primary key value, which InnoDB uses to find the row in the clustered index. - `BTREE` is the **only** `index_type` InnoDB permits. The manual's rule is that when you name a type the engine cannot build but it has another type that gives the same results, it uses that one. So `USING HASH` on an InnoDB table gives you a B-tree, and `SHOW INDEX` reports `BTREE`. ## FULLTEXT: an inverted index for words - An InnoDB `FULLTEXT` index stores, for each word, the documents containing it and the word's positions. It works on `CHAR`, `VARCHAR` and `TEXT` columns and always indexes the whole column; a prefix length is ignored. - You query it with `MATCH(col, ...) AGAINST('...')`. The `MATCH()` column list must match the column list of some `FULLTEXT` index on the table exactly. - With no modifier, or with `IN NATURAL LANGUAGE MODE`, results are ranked by relevance. `IN BOOLEAN MODE` adds operators such as a leading `+` (must contain), a leading `-` (must not contain) and a trailing `*` (prefix match), and does not sort by relevance on its own. - Words shorter than `innodb_ft_min_token_size` (default 3) and words on the stopword list are not indexed. That variable is not dynamic: changing it needs a server restart and a rebuild of the full-text indexes. - InnoDB applies full-text changes at commit, so a full-text search sees only committed rows. ## SPATIAL: an R-tree for geometry - `SPATIAL INDEX` builds an **R-tree**, which groups shapes by their bounding rectangles so a search can skip whole regions. - The indexed column must be a single geometry column declared `NOT NULL`. Prefix lengths are not allowed, and a spatial index cannot be a primary key or a unique index. - The optimizer considers a spatial index **only when the column is SRID-restricted**, that is, declared with an `SRID` attribute such as `SRID 4326`. A spatial index on a column without one is still maintained on every write, but the optimizer ignores it. Since 8.4 the optimizer compares index values with calculations suited to the column's SRID; earlier releases always used Cartesian calculations. - A plain `INDEX` on a geometry column builds a B-tree instead, which is useful for exact-value lookups and useless for range searches. ## What InnoDB does not give you 1. **No user-created hash index.** Hash indexes exist in the `MEMORY` engine, where `HASH` is the default type. 2. **The adaptive hash index is not an index kind.** It is a global, internal cache that InnoDB can build over hot B-tree pages, switched by `innodb_adaptive_hash_index`. MySQL 8.4 changed that variable's default from `ON` to `OFF`. 3. **No functional `FULLTEXT` or `SPATIAL` parts.** Expression key parts work in B-tree indexes only. ## Checking what you actually built `SHOW INDEX FROM tbl` lists one row per key part. Its `Index_type` column reports the method (for example `BTREE` or `FULLTEXT`), and `Visible` and `Expression` show visibility and functional key parts. Interviewers ask this because candidates who learned on another engine often assume a hash index or a heap table exists in InnoDB when neither does.
- In MySQL with InnoDB, why can a full-text search miss a three-letter product code such as 'abc' or the word 'the'?InnoDB indexes only words of at least `innodb_ft_min_token_size` characters (default 3) and skips words on its stopword list, so a two-letter code or a stopword never reaches the index. Raising or lowering the size needs a server restart and a rebuild of the `FULLTEXT` index; stopwords are controlled with `innodb_ft_enable_stopword` and the stopword-table variables, and changing those also needs an index rebuild.
- In MySQL 8.4, why might the optimizer ignore an existing SPATIAL index on an InnoDB geometry column?The optimizer considers a `SPATIAL` index only on an SRID-restricted column, one declared with an `SRID` attribute so every value shares one spatial reference system. Without it the index is still maintained on writes but is never used. The fix is to check `SELECT DISTINCT ST_SRID(col)`, make the values agree, redefine the column with `SRID n` and recreate the index.
- USING HASH gives an InnoDB table a real hash index
- A FULLTEXT index speeds up LIKE '%word%' searches
- InnoDB stores rows in a heap and indexes point at it
- Any SPATIAL index is used whatever the column's SRID
- The adaptive hash index is something you create per table
Assumes MySQL 8.4, which changed the innodb_adaptive_hash_index default from ON to OFF and made SPATIAL index comparisons follow the column's SRID instead of always using Cartesian math.