Query Optimization and Execution Plans

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

573 associated posts292 primary posts

Performance Tuning

Is leading an index with a BIT column always bad?

“Throughout history, slow queries are the normal condition of man. Indexes which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people who don't think bit columns are selective enough to…

Read more about Is leading an index with a BIT column always bad? 5 comments — Join the discussion
Performance Tuning

Finding Tables with Nonclustered Primary Keys and no Clustered Index

i've seen this happen
Especially if you've just inherited a database, or started using a vendor application. This can also be the result of inexperienced developers having free reign over index design.

Unless you're running regular health checks on your indexes with something like our sp_BlitzIndex® tool, you might not catch immediately that you have a heap of HEAPs in your database.

Read more about Finding Tables with Nonclustered Primary Keys and no Clustered Index 31 comments — Join the discussion
Performance Tuning

New Cardinality Estimator, New Missing Index Requests

During some testing with SQL Server 2014's new cardinality estimator, I noticed something fun: the new CE can give you different index recommendations than the old one. I'm using the public Stack Overflow database export, and I'm running this Jon Skeet comparison query from Data.StackExchange.com. (Note that it has something a little tricky at the…

Read more about New Cardinality Estimator, New Missing Index Requests 3 comments — Join the discussion
Performance Tuning

Indexing for GROUP BY

It's not glamorous And on your list of things that aren't going fast enough, it's probably pretty low. But you can get some pretty dramatic gains from indexes that cover columns you're performing aggregations on. We'll take a quick walk down demo lane in a moment, using the Stack Overflow database. Query outta nowhere! [crayon-6a70a953d8c99430068784/]…

Read more about Indexing for GROUP BY 9 comments — Join the discussion

Careful Testing the 2014 Cardinality Estimator with Trace Flags

When you migrate to SQL Server 2014, rather than turning on the new cardinality estimator right away, I recommend sticking with the old cardinality estimator initially by leaving your database in SQL Server 2012 compatibility mode. Let the database settle down for a few weeks, let folks air out their grievances, and give yourself some time to make sure the system is working at least as well as the old system.

Read more about Careful Testing the 2014 Cardinality Estimator with Trace Flags 34 comments — Join the discussion
Performance Tuning

When does a Query Get Trivial Optimization?

We had some great questions about trivial execution plans in SQL Server in our Advanced Querying and Indexing class a few weeks ago. Here's a little glimpse into what we talked about.

For really simple queries, SQL Server can use "trivial optimization". If there's a very limited number of ways to run the query, why do a bunch of fancy, CPU burning cost-based optimization? Just chuck the plan at the query and let it go!

Read more about When does a Query Get Trivial Optimization? 6 comments — Join the discussion
Performance Tuning

Are Index ‘Included’ Columns in Your Multi-Column Statistics?

When you create an index in SQL Server with multiple columns, behind the scenes it creates a related multi-column statistic for the index. This statistic gives SQL Server some information about the relationship between the columns that it can use for row estimates when running queries.

But what if you use 'included' columns in the index? Do they get information recorded in the statistics?

Read more about Are Index ‘Included’ Columns in Your Multi-Column Statistics? 3 comments — Join the discussion

Upgrading to SQL Server 2014: Frequently Asked Questions

Can I upgrade an existing instance without migrating?

This is nothing against SQL Server 2014, but I can't stand in-place upgrades. Over the years I've had in-place upgrades work flawlessly on a few instances, and then had an install issue cause it to fail in the middle on other instances. Usually the critical instances, just because I'm not always lucky. And when upgrade fails, it doesn't always roll back completely, or allow you to just re-run it. You may be down for a good long time.

Read more about Upgrading to SQL Server 2014: Frequently Asked Questions 36 comments — Join the discussion
Performance Tuning

Using “OR” and “IN” with SQL Server’s Filtered Indexes

You can't do everything with filtered indexes in SQL Server. For instance, you can't create the following index:
[crayon-6a70a953ddd0d535383833/]
If you try, you'll get the error message:
[crayon-6a70a953ddd1c169332533/]
Instead, you can use 'IN' and create the index this way:
[crayon-6a70a953ddd20693256293/]
That works-- and good news, even queries written with 'OR' can use that filtered index, because SQL Server is clever like that. Here's an execution plan that shows it in action.

Read more about Using “OR” and “IN” with SQL Server’s Filtered Indexes 4 comments — Join the discussion
Performance Tuning

How many CPUs is my parallel query using in SQL Server?

Parallelism can be confusing. A single query can have multiple operators that run at the same time. Each of these operators may decide to use multiple threads. You set SQL Server's "max degree of parallelism" to control the number of processors that can be used, but it's not immediately obvious what this means. Does this setting…

Read more about How many CPUs is my parallel query using in SQL Server? 4 comments — Join the discussion

SQL Server’s Cost Threshold for Parallelism

"Should a query get to use more than one CPU core?" That's an important question for your SQL Server. If you're not sure what parallelism is, get started by exploring the mysteries of CXPACKET with Brent. He'll introduce you to the the setting, 'Cost Threshold for Parallelism'.
Let's test Cost Threshold for Parallelism
I generate an estimated execution plan for the following query. I'm running against a copy of the StackOverflow database that doesn't have many indexes.

Read more about SQL Server’s Cost Threshold for Parallelism 20 comments — Join the discussion