InnoDBtable has a special index called the clustered index where the data for the rows is stored. Typically, the clustered index is synonymous with the primary key. To get the best performance from queries, inserts, and other database operations, you must understand how
InnoDBuses the clustered index to optimize the most common lookup and DML operations for each table.
- When you define a
PRIMARY KEYon your table,
InnoDBuses it as the clustered index. Define a primary key for each table that you create. If there is no logical unique and non-null column or set of columns, add a new auto-increment column, whose values are filled in automatically.
- If you do not define a
PRIMARY KEYfor your table, MySQL locates the first
UNIQUEindex where all the key columns are
InnoDBuses it as the clustered index.
- If the table has no
PRIMARY KEYor suitable
InnoDBinternally generates a hidden clustered index named
GEN_CLUST_INDEXon a synthetic column containing row ID values. The rows are ordered by the ID that
InnoDBassigns to the rows in such a table. The row ID is a 6-byte field that increases monotonically as new rows are inserted. Thus, the rows ordered by the row ID are physically in insertion order.