liveBook · Manning

Nonclustered index

As explained in 100 SQL Server Mistakes and How to Avoid Them

A nonclustered index is a separate index structure that stores keys and row locators without determining the table's physical row order.

A nonclustered index is a separate B-tree built on one or more table columns. Its leaf level stores row locators rather than the table’s full data pages, so it can speed joins, filters, and aggregations without changing the physical order of table data. A table can therefore have multiple nonclustered indexes. section 11.0 section 4.2

The locator points to a heap or to a clustered index. When a clustered index exists, its key is copied into every nonclustered index locator; a wide clustered or primary key is therefore replicated widely. INCLUDE columns are an exception to the pointer-only idea: their values are stored in the nonclustered index alongside the locator. section 4.2 section 4.2

Use a nonclustered index deliberately:

  • Index columns frequently used to filter sales orders, such as an order date. section 4.2
  • Index foreign-key columns used in joins, such as SalesAreaID and CustomerID, to support more efficient join operations. section 4.2
  • Balance read improvements against disk or memory consumption and slower writes; up to 256 nonclustered indexes are supported, but excess indexes add maintenance work. section 11.0

Bulk-load trap

Loading millions of rows into an archive table with reporting indexes updates both table pages and every nonclustered index. The extra I/O, transaction-log work, page splits, and fragmentation can make the load substantially slower. For a large load, disable the nonclustered indexes, insert the rows, then rebuild them; do not disable the clustered index, because doing so makes the table inaccessible. section 11.9 section 11.9

Figure 11.11 Inserting new rows into a table with nonclustered indexes

Use the insertion diagram to see why every added row creates work in the table and in each separate nonclustered index. section 11.9

Inspecting an index

Find a named index and its index_id in sys.indexes, then use DBCC IND to locate its pages. DBCC PAGE can inspect a selected page after enabling trace output, making the separate index structure and its stored entries tangible. section 4.2 section 4.2

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Defines B-tree leaf locators, included values, clustered-key replication, and bulk-load maintenance, then demonstrates page inspection.
Examples
Uses SalesOrderDate and foreign-key indexes, index-page DBCC inspection, and the ImpressionArchive bulk-load scenario.
What it assumes
Introduces heaps, clustered indexes, and B-tree locators from zero while using tables, keys, joins, and INSERT operations.
What you can do after
Operate/configure it by selecting query-supporting indexes and disabling then rebuilding nonclustered indexes around large bulk loads.
How it connects
Connects clustered indexes, heaps, primary keys, foreign keys, INCLUDE columns, joins, bulk loads, and transaction logs.

Also explained in

Compare how 2 books explain nonclustered index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.