Index Design and Access Paths

Clustered and nonclustered indexes, keys, included columns, and access paths.

113 associated posts109 primary posts

Production DBA

Office Hours Speed Round, Text Edition

Got questions for me? Post 'em at https://pollgab.com/room/brento and upvote the ones you'd like to see me cover. I filter out the ones that are too short for video answers, and here's the latest batch:

Corrupted: Should DBCC CheckDB be run on secondary replicas in AG as well ? Is it recommended to attach corrupted database to Prod server, to check how your integrity check job will react ?

Read more about Office Hours Speed Round, Text Edition 20 comments — Join the discussion
Performance Tuning

When Should You Use DESC in Indexes?

The short answer is that if your query orders columns by a mix of ascending and descending order, back to back, then the index usually needs to match that same alternating order. Now, for the long answer. When you create indexes, you can either create them in ascending order - which is the default: [crayon-6a6ece8c5ab9d946850757/] Or…

Read more about When Should You Use DESC in Indexes? 8 comments — Join the discussion
Performance Tuning

When Do I Need to Use DESC in Indexes?

If I take the Users table from any Stack Overflow database, put an index on Reputation, and write a query to find the top 100 users sorted by reputation, descending:
[crayon-6a6ece8c5b169895136389/]
It doesn't matter whether the index is sorted ascending or descending. SQL Server goes to the end of the index and starts scanning backwards:

Read more about When Do I Need to Use DESC in Indexes? 4 comments — Join the discussion
Performance Tuning

[Video] How to Think Like Clippy (Subtitle: Watch Brent Wear Another Costume)

Remember Clippy, the Microsoft Office assistant from the late 1990s? He would pop up at the slightest provocation and offer to help you do something - usually completely unrelated to the task you were trying to accomplish. Sadly, the Office team told Clippy that he didn't make the stack rankings cut, so he relocated over…

Read more about [Video] How to Think Like Clippy (Subtitle: Watch Brent Wear Another Costume) 3 comments — Join the discussion

[Video] How to Think Like the SQL Server Engine: All-Demo Edition

You’ve heard of my free How to Think Like the Engine class, and maybe you even started watching it, but…it has slides, and you hate slides.

Wanna see me do the whole thing in Management Studio, starting with an empty query window and writing the whole thing out from scratch live? This session is for you.

Read more about [Video] How to Think Like the SQL Server Engine: All-Demo Edition 3 comments — Join the discussion
Performance Tuning

Things to Consider When SQL Server Asks for an Index

One of the things I love about SQL Server is that during query plan compilation, it takes a moment to consider whether an index would help the query you're running. Regular blog readers will know that I make a lot of jokes about the quality of these recommendations - they're often incredibly bad - but even bad suggestions can be useful if you examine 'em more closely.

Read more about Things to Consider When SQL Server Asks for an Index Be the first to comment
Performance Tuning

WHERE GETDATE() BETWEEN StartDate AND EndDate Is Hard to Tune.

Say you've got a memberships (or policies) table, and each membership has start & end dates:
[crayon-6a6ece8c5cbdd601985465/]
If all you need to do is look up the memberships for a specific UserId, and you know the UserId, then it's a piece of cake. You put a nonclustered index on UserId, and call it a day.

Read more about WHERE GETDATE() BETWEEN StartDate AND EndDate Is Hard to Tune. 19 comments — Join the discussion
Performance Tuning

Building SQL ConstantCare: Let’s Tune an Index in Postgres.

This week, our SQL ConstantCare® back end services started having some query timeout issues. Hey, we're database people - we can do this, right? Granted, I work with Microsoft SQL Server most of the time, but we host our data in Amazon Aurora Postgres - is a query just a query and an index just…

Read more about Building SQL ConstantCare: Let’s Tune an Index in Postgres. 9 comments — Join the discussion
Performance Tuning

How to Think Like the Engine: Index Column Order Matters a LOT.

We've been working with the clustered index of the Users table, which is on the Identity column - starts at 1 and goes up to a bajillion:

And in a recent episode, we added a wider nonclustered index on LastAccessDate, Id, DisplayName, and Age:
[crayon-6a6ece8c5ecc6573827247/]
Whose leaf pages look like this:

Read more about How to Think Like the Engine: Index Column Order Matters a LOT. 2 comments — Join the discussion