Query Optimization and Execution Plans

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

573 associated posts292 primary posts

T-SQL & Development

Coming in Entity Framework 9: Better Query Parameterization

Hallelujah. With current versions of Entity Framework, when developers add a mix of parameters and specific values to their query like this: [crayon-6a71f8e80f7ed862643737/] See how part of the filter is hard-coded (".NET Blog") while the other part of the filter is dynamically generated, an ID the user is looking for? That causes Entity Framework to…

Read more about Coming in Entity Framework 9: Better Query Parameterization 22 comments — Join the discussion

[Video] Office Hours: Ask Me Anything About Azure and Microsoft Databases

Back at home in the office, time to settle in with a nice caffeine-free Diet Coke and go through your top-voted questions from https://pollgab.com/room/brento. Why caffeine-free? Because I slug multiple coffees first thing in the morning when I wake up (usually around 3am-4am), and by the time I stream with y'all, I don't need any more go juice.

Read more about [Video] Office Hours: Ask Me Anything About Azure and Microsoft Databases 4 comments — Join the discussion
T-SQL & Development

Option Recompile is a Magic Turbo Button That Actually Works.

I didn't say that - Guy Glantser did.

Guy Glantser is an Israeli SQL Server guru with a ton of great presentations on YouTube. I've had the privilege of hanging out with him in person a bunch of times over the year, and I'll always get excited to do it again. He's not just smart, but he's friendly and funny as hell.

Read more about Option Recompile is a Magic Turbo Button That Actually Works. 29 comments — Join the discussion
Performance Tuning

What Happens When Multiple Queries Compile at Once?

An interesting question came in on PollGab. DBAmusing asked: If a query takes 5-7s to calculate the execution plan (then executes <500ms) if multiple SPIDS all submit that query (different param values) when there's no plan at start, does each SPID calc the execution plan, one after the other after waiting for the prior SPID…

Read more about What Happens When Multiple Queries Compile at Once? 5 comments — Join the discussion
Performance Tuning

Find Recent Superstars: Answers & Discussion

Your query exercise for this week was to write a query to find users created in the last 90 days, with a reputation higher than 50 points, from highest reputation to lowest. Because everyone's Stack Overflow database might be slightly different, we had to start by finding the "end date" for our query. I'm working with the 2018-06 export that I use in my training classes, so here's my end date:

Read more about Find Recent Superstars: Answers & Discussion 1 comment — Join the discussion
Performance Tuning

Query Exercise: Finding Long Values Faster

Our developers have come to us with a problem query that isn't as fast as they'd like. Using any Stack Overflow database:
[crayon-6a71f8e82352d832450302/]
It has an index, but SQL Server refuses to use that index in the execution plan:

If we force the index with a query hint, we do indeed get dramatically lower logical reads. In my particular database's case, the clustered index scan is 141,573 logical reads - but scanning the DisplayName index alone is just 38,641 logical reads.

Read more about Query Exercise: Finding Long Values Faster 45 comments — Join the discussion
Performance Tuning

Improving Cardinality Estimation: Answers & Discussion

Your challenge for last week was to take this Stack Overflow database query to show the top-ranking users in the most popular location:
[crayon-6a71f8e8243b4083870444/]
And make it read less pages only by tuning the query? You weren't allowed to make index or server changes, and you weren't allowed to hard code the location in the query since it might change over time.
The Core of the Problem
The main problem is that when we run a statement (like SELECT), SQL Server:

Read more about Improving Cardinality Estimation: Answers & Discussion 16 comments — Join the discussion
Performance Tuning

Query Exercise: Improving Cardinality Estimation

Your challenge for this week is to tune a query. Say Stack Overflow has a dashboard that shows the top-ranking users in their most popular location. It's even got an index to support it:
[crayon-6a71f8e824bf8985250582/]
You can test it with any version of the Stack Overflow database. To test it, we'll turn on a couple of tuning options:
[crayon-6a71f8e824c03308439652/]
The actual execution plan does use our index - not just once, but twice:

Read more about Query Exercise: Improving Cardinality Estimation 33 comments — Join the discussion
Performance Tuning

Finding Tagged Questions Faster: Answers & Discussion

Your query exercise was to take this Stack Overflow query to find the top-voted questions for any given tag:
[crayon-6a71f8e82581c926180361/]
That's currently using this index in its execution plan:
[crayon-6a71f8e82582a773102921/]
And answer 3 questions:

What kinds of tags will perform worse than others for this query?
Could you change the query to perform better?
Could you change the indexes to perform better, without changing the table structure?

Read more about Finding Tagged Questions Faster: Answers & Discussion 11 comments — Join the discussion