Query Optimization and Execution Plans

How SQL Server compiles, optimizes, and executes individual queries.

573 associated posts292 primary posts

Performance Tuning

Filtered Indexes and Variables: Less Doom and Gloom

It Is Known
That when you use filtered indexes, they get ignored when your queries are parameterized. This is a Plan Caching Thing©, of course. The simplest example is a bit column with a filtered index. If your index is on WHERE Bit = 1, it doesn't have data for WHERE Bit = 0. That index would only be suitable for one variation of the query, so caching a plan that uses an index which can't be reused for every variation isn't feasible.

Read more about Filtered Indexes and Variables: Less Doom and Gloom 5 comments — Join the discussion
Performance Tuning

Filtered Statistics Follow-up

During our pre-con in Seattle
A really sharp lady brought up using filtered statistics, and for a good reason. She has some big tables, and with just 200 histogram steps, you can miss out on a lot of information about data distribution when you have millions or billions of rows in a table. There's simply not enough room to describe it all accurately, even with a full scan of the stats.

Read more about Filtered Statistics Follow-up 5 comments — Join the discussion

Ten Ways to Set MAXDOP

Whenever I work with SQL Server, I'm amazed at how many ways there are to influence its behavior. For example, take the maximum degree of parallelism for a query. Just offhand, I thought of ten different ways you can tweak it:

1. At the server level with SSMS. In SSMS, right-click on the server, click Properties, Advanced, scroll down into the Parallelism section, and set MAXDOP to 1. Click OK. (Be aware that this blows your plan cache instantly.)

Read more about Ten Ways to Set MAXDOP 15 comments — Join the discussion
Performance Tuning

Indexing Temp Tables

People often don't give this thought
Which is a shame, because I see people sticking fairly large amount of data into temp tables. On the rare occurrence that I do see them indexed, it's a nonclustered index on a column or two. The optimzer promptly ignores this index while you select 10 columns and join 10,000 rows to another temp table with another ignored nonclustered index on it.

Read more about Indexing Temp Tables 46 comments — Join the discussion
Performance Tuning

What’s the Difference Between Estimated and Actual Execution Plans?

I'm going to use the dbo.Users table in the StackOverflow demo database and run a pretty simple query: [crayon-6a70eaad8ed75790257008/] First, hit Control-L in SSMS and get the estimated execution plan. Here it is: Click on the plan, and hover your mouse over the various operators. You'll notice that almost all of the fields are prefixed…

Read more about What’s the Difference Between Estimated and Actual Execution Plans? 2 comments — Join the discussion
Performance Tuning

Using Plan Guides to Remove OPTIMIZE FOR UNKNOWN Hints

Say you've got an application that has tons of OPTIMIZE FOR UNKNOWN hints in the T-SQL, and you're getting bad query plans.

We're going to use the same StackOverflow query (and the same index on Reputation) that I demoed in the post Why Is This Query Sometimes Fast and Sometimes Slow? This technique produces a query that will produce two different execution plans depending on the Reputation parameter.

Read more about Using Plan Guides to Remove OPTIMIZE FOR UNKNOWN Hints 7 comments — Join the discussion
Performance Tuning

Why Is This Query Sometimes Fast and Sometimes Slow?

You swear you didn't change anything, but all of a sudden the SQL Server is going doggone slow. What happened? Parameter sniffing might be the problem, and to explain it, let's see how it works. I'm going to use the StackOverflow database - particularly, the Users table that I demo in How to Think Like the…

Read more about Why Is This Query Sometimes Fast and Sometimes Slow? 11 comments — Join the discussion

My How to Think Like the Engine Class is Now Free – and Open Source

You’re a developer or a DBA, and you’re comfortable writing queries to get the data you need. You’re much less comfortable trying to design the right indexes for your database server. You've never had a formal database internals class, and you don't really have the patience to read a book on it - but you want a quick foundation.

Read more about My How to Think Like the Engine Class is Now Free – and Open Source 21 comments — Join the discussion