145. SQL Indexing: Why Is Your Query So Slow?

Content type
Video

145. SQL Indexing: Why Is Your Query So Slow?

How do SQL indexes actually work, and why does SQL Server sometimes ignore the index you created? Christian explains SQL indexing from the ground up, with Jelle asking the questions.

“SQL Server will notice when it’s easy or non-beneficial to use an index and will actually not use it, but you still have to maintain it.”

Christian starts with how SQL Server stores data in 8 KB pages, and how a balanced tree on top of those pages makes searching a million rows take only a few steps.

We cover:

  • The difference between a clustered and a non-clustered index

  • Why an auto-increment ID usually makes a better clustered key than a GUID

  • What you get for free when you set a primary key

  • Demo in the AdventureWorks database: execution plans, key lookups and index statistics

  • Why this still matters when you use Entity Framework

The main point: whether an index helps depends on your data. Searching for white products uses the color index, but searching for black products scans the whole table, because the statistics show there are far more black products. And every index costs you on every insert, update and delete, and in storage.

Want a follow-up on included columns, filtered indexes, columnstore or vector indexes? Let us know in the comments on YouTube.

More videos

Stay up to date with our Betatalks and tech updates!

Sign up and receive a biweekly update with the latest knowledge and developments.