Query Optimization and Execution Plans

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

573 associated posts292 primary posts

What To Do If sp_BlitzFirst Warns About High Compilations

Compiles Aren't The Same As Recompiles
If you're seeing high RECOMPILES, this post isn't for you. We'll talk about the differences between compiles and recompiles, but not how to troubleshoot recompiles.

Recompiles mean one of two obvious things: You have a RECOMPILE hint at the stored procedure or statement level, or SQL found a reason that a new query plan had to be compiled, despite the presence of what was once a perfectly good query plan.

Read more about What To Do If sp_BlitzFirst Warns About High Compilations 5 comments — Join the discussion

How to Select Specific Columns in an Entity Framework Query

One of the most frequent complaints that I hear when presenting to DBAs about Entity Framework is that it's "slow" and that "developers should be endlessly tortured for using it". Ok, the second part I just made up but the sentiment exists. DBAs just don't like developers using Entity Framework and with good reason. Entity Framework can make…

Read more about How to Select Specific Columns in an Entity Framework Query 42 comments — Join the discussion
News & Opinion

Announcing PasteThePlan.com: An Easier Way to Share Execution Plans

Since the dawn of man, people have struggled with sharing execution plans with each other for performance tuning. Now, it's easy. First, get yourself a plan: Get the estimated execution plan by hitting control-L or clicking Query, Display Estimated Plan. Right-click on the graphical plan, and click View XML. Copy all of that. Or even…

Read more about Announcing PasteThePlan.com: An Easier Way to Share Execution Plans 18 comments — Join the discussion
News & Opinion

Bad Idea Jeans Week: Dynamically Generating Long Queries

As part of an experiment, I needed to build a really long query. (Don't ask.)

From another recent experiment, I know that SQL Server won't let a query return more than 65,535 columns. I set about writing a one-line query that would return 65,535. I'm a big fan of writing the simplest reproduction scripts possible - I don't want them to rely on tables if they don't have to - so we'll start a CTE like this:

Read more about Bad Idea Jeans Week: Dynamically Generating Long Queries 12 comments — Join the discussion
Performance Tuning

Query Tuning Week: How to Run sp_BlitzCache on a Single Query

The most popular way of using sp_BlitzCache® is to just run it - by default, it shows you the top 10 most CPU-intensive queries that have run on your server recently. Plus, it shows you warnings about each of the queries - if they're missing indexes, experiencing parameter sniffing issues, running long, running frequently, doing implicit conversion, you name it.

Read more about Query Tuning Week: How to Run sp_BlitzCache on a Single Query 5 comments — Join the discussion
Performance Tuning

Question From Office Hours: SQL Handle vs. Plan Handle

Great question!
We recently added some columns to sp_BlitzCache to help you remove undesirable plans from the cache. Doing this will force SQL to come up with a new plan, or you know, just re-create the old plan. Because that just happens sometimes. I answered it, but I wasn't happy with my answer. So here's a better one!

Read more about Question From Office Hours: SQL Handle vs. Plan Handle 12 comments — Join the discussion
Performance Tuning

How to Start Troubleshooting Parameter Sniffing Issues

When a query is sometimes fast and sometimes slow, for the same input parameters, and you swear nothing else in the environment is changing, that's often a case of parameter sniffing.

After SQL Server starts up and you run a stored procedure, SQL Server builds an execution plan for that proc based on the first set of parameters that are passed in.

Read more about How to Start Troubleshooting Parameter Sniffing Issues 17 comments — Join the discussion
Performance Tuning

Query Tuning Week: How to Start Troubleshooting a Slow Stored Procedure

When you need to find out why a stored procedure is running slow, here's the information to start gathering: Check to see if the plan is in the cache. Run sp_BlitzCache® and use several different @sort_order parameters - try cpu, reads, duration, executions. If you find it in your top 10 plans, you can view…

Read more about Query Tuning Week: How to Start Troubleshooting a Slow Stored Procedure 7 comments — Join the discussion
Performance Tuning

An Introduction to Query Memory

Microsoft has been quietly making some amazing improvements for performance tuners in SQL Server 2012, 2014, and 2016. This week, we're going to introduce you to just how awesome they are. (They being the improvements, not Microsoft. You already knew they were awesome.)

Using the freely available StackOverflow database, let's start with a simple query - SELECT * FROM Users:

Read more about An Introduction to Query Memory 9 comments — Join the discussion
Performance Tuning

Can Adding an Index Make SQL Server 2016…Worse?

Using the StackOverflow database, let's check out Krock's query. He's a competitive fella, and he's looking to find users who signed up for StackOverflow after he did, but who have a higher reputation than he does. I'm going to simplify the query a little here: [crayon-6a70d4db06ae0297591235/] The Users table has a clustered index on the Id…

Read more about Can Adding an Index Make SQL Server 2016…Worse? 9 comments — Join the discussion