Indexes and performance
Clustered and nonclustered indexes
Faster reads have write costs.
Understand the concept
An index is a data structure supporting access patterns. A rowstore clustered index contains table rows at its leaf level; a table has at most one. A table without one is a heap. Nonclustered indexes store keys and a row locator. A wide clustered key can increase other indexes because it is used as their locator.
Indexes cost writes, storage and maintenance. Indexing every column is not a solution. A scan can be correct on tiny lab tables; seeks are not inherently cheaper. Evaluate filters, ordering and selected columns against the real workload. PRIMARY KEY and clustered index are distinct concepts even when defaults combine them.
Design an access path for a workload.
Treating every scan as bad and every seek as good.
One topic. Three levels of depth.
SELECT name, type_desc FROM sys.indexes WHERE object_id = OBJECT_ID(N'lab.Product');Inspect existing index types.
This page does not execute SQL. Practise in the downloadable SqlStudioLab using SSMS. Before rerunning temporary-table examples, remove only the relevant #table. Views, procedures and functions create persistent lab objects; never use a production connection.
Try it yourself first.
List existing indexes on Customer.
Show the solution and explanation
SELECT name, type_desc, is_unique FROM sys.indexes WHERE object_id = OBJECT_ID(N'lab.Customer');This metadata read does not create an index.