Query Optimization and Execution Plans

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

573 associated posts292 primary posts

How to Query JSON Data Quickly in SQL Server, Part 2: SQL Server 2025

SQL Server 2025 and .NET 10 bring several new improvements to storing JSON natively in the database and querying it quickly. On the SQL Server 2025 side, the two big ones are the new native JSON indexes and the new JSON_CONTAINS function. Let's see their improvements in action. On the .NET 10 side, EF 10…

Read more about How to Query JSON Data Quickly in SQL Server, Part 2: SQL Server 2025 11 comments — Join the discussion
Performance Tuning

Query Plan Pop Quiz Answers 2 and 3: I’ve Got Good News and Bad News.

In the Query Plan Pop Quiz, questions 2 and 3 asked you about what the sizes of arrows on query plans meant. The good news is that almost all of you got Question 2 right, but the bad news is that the vast majority of you got Question 3 completely incorrect, and the saddest part of that is that you've been using that inaccurate knowledge to guide your query tuning - and wasting your time.

Read more about Query Plan Pop Quiz Answers 2 and 3: I’ve Got Good News and Bad News. 5 comments — Join the discussion
Performance Tuning

Query Plans Pop Quiz Answer #1: Costs are Garbage.

In last week's Query Plans Pop Quiz, the first question was, someone hands you these two queries and you get their estimated plans to decide which query to tune. Perhaps you get the estimated plans from SSMS, or from sp_BlitzCache, or from your monitoring tool. The question was, which query should you focus on tuning?…

Read more about Query Plans Pop Quiz Answer #1: Costs are Garbage. 4 comments — Join the discussion

SSMS v22 Query Hint Recommendation Tool: The Invasion of the Query Hints

SQL Server Management Studio 22 Preview 3 is out, and it brings with it a new Query Hint Recommendation tool. Start by highlighting the query you want to test, then click Tools, Query Hint Recommendation Tool. It slides out a new pane on the right hand side: The maximum tuning time defaults to 300 seconds,…

Read more about SSMS v22 Query Hint Recommendation Tool: The Invasion of the Query Hints 24 comments — Join the discussion
Performance Tuning

Query Plans Pop Quiz: Three Simple Questions

Question 1: Pick the Problematic Plan: someone hands you a pair of queries, and you get the estimated query plans. (Perhaps you get the estimated plans from SSMS, or from sp_BlitzCache, or from your monitoring tool.) Which one of these two should you focus on tuning first, Query 1 or Query 2?

Question 2: on an estimated plan, what does the thickness of the colored arrow represent?

Read more about Query Plans Pop Quiz: Three Simple Questions 28 comments — Join the discussion
Production DBA

Set MAXDOP in Azure SQL DB or You’ll Get This Cryptic Error.

Max Degrees of Parallelism (MAXDOP) tells the database engine, "If you decide to parallelize a query, go parallel with this many worker threads."

(It's a little more complex than that - there is also a coordinating thread, plus a single plan might have multiple parallel zones that each consume MAXDOP worker threads, but for the sake of this blog post, let's keep it simple.)

Read more about Set MAXDOP in Azure SQL DB or You’ll Get This Cryptic Error. 1 comment — Join the discussion

[Video] Office Hours: Database Questions & Answers

In between clients, I hopped onto my Twitch channel to take your top-voted questions from https://pollgab.com/room/brento. If you'd like to get notified whenever I do one of those live streams, you can follow my channel for free and you'll get email notifications automatically. I do 'em whenever I have time, usually about once a week when I'm at home.

Read more about [Video] Office Hours: Database Questions & Answers 1 comment — Join the discussion

[Video] Office Hours on the Beach in Cancun

In retrospect, I should have put on sunscreen - even for just a 20-minute Office Hours session on the beach! Dang, that sun is powerful. Let's go through your top-voted questions from https://pollgab.com/room/brento. This is a 360-degree video, so if you watch it on YouTube, you'll be able to pan the camera around and take a little virtual vacation with me.

Read more about [Video] Office Hours on the Beach in Cancun 4 comments — Join the discussion

Review: SSMS Copilot vs Other AI Tools

I use AI tools constantly in my work, so when the new SSMS v21 came out with Copilot support, I was excited to try it. The short story is that the user interface is fine, and it doesn't feel buggy, but the advice is simply terrible compared to other LLMs due to the prompt Copilot is using. You're better off using tools like ChatGPT, Gemini, and Claude directly for now.

Read more about Review: SSMS Copilot vs Other AI Tools 25 comments — Join the discussion

How SQL Server 2025’s Optional Parameter Plan Optimization Works

About three years ago, SQL Server 2022 introduced Parameter-Sensitive Plan Optimization (PSPO). At the time, I explained that it didn't work particularly well, and went so far as to pronounce PSPO in a rather unflattering way. I wouldn't suggest that anyone turn it off - it's fine, just fine - but it isn't powerful enough, and poses serious challenges for monitoring and plan cache analysis.

Read more about How SQL Server 2025’s Optional Parameter Plan Optimization Works 15 comments — Join the discussion
Performance Tuning

SQL Server 2022 Finally Fixed a SQL Server 2008 Query Plan Bug!

For yeeeeeears, when I've explained execution plans, part of my explanation has included the instructions, "Read the plan from right to left, top to bottom, looking for the place where the estimates vs actuals are suddenly way off." Here's an example:

Things seem to be going okay on the query plan until you hit the key lookup, which brought back 13 rows of an estimated 19,452. That would appear to be a pretty doggone bad estimate.

Read more about SQL Server 2022 Finally Fixed a SQL Server 2008 Query Plan Bug! 5 comments — Join the discussion