liveBook · Manning

Clustered index

As explained in Learn SQL in a Month of Lunches

A clustered index is a table-organizing structure that determines the physical order in which rows are stored on a storage medium.

A clustered index controls the physical order of rows on storage media. Without an index or constraint, rows have no particular order, so queries may need to read every row to find matching data. section 19.3.1 section 19.3

A telephone-book analogy

A telephone book is sorted by surname and then first name, with each person’s telephone number and address beside the name. If the book were a table, that sorting would be its clustered index. Because rows can be physically ordered only one way, a table can have only one clustered index. section 19.3.1

Keep these practical points in mind:

  • The clustered index effectively becomes the table rather than remaining a separate object. section 19.3.1
  • Choose columns that are commonly used, because the table can be sorted in only one way. section 19.3.1
  • Primary-key columns are common choices because joins often read related primary and foreign key values. section 19.3.1
  • In MySQL, creating a PRIMARY KEY for the shipment table automatically created its clustered index. section 19.3.1
Figure 19.1 The three indexes of the shipment table in the sqlnovel database, viewed in MySQL Workbench

Inspecting the shipment table’s indexes in MySQL Workbench helps connect the clustered-index idea with a concrete database design. section 19.3.1

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains physical ordering, scan implications, key selection, and table identity, but does not provide clustered-index creation syntax.
Examples
Uses a sorted telephone book and the MySQL shipment table’s PRIMARY index to illustrate physical row organization.
What you can do after
Evaluate clustered-index choices by considering common query columns, primary-key joins, physical ordering, and table-wide scan costs.
How it connects
Connects clustered indexes with primary keys, foreign keys, joins, nonclustered indexes, and relational database management systems.

Also explained in

Compare how 2 books explain clustered index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.