Query Optimization and Execution Plans

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

573 associated posts292 primary posts

Performance Tuning

Filtered Indexes and Dynamic SQL

I’ve been told that an attendee at the PASS Summit pre-conference event asked about using dynamic SQL to get around some of the problems with filtered indexes. I’m not entirely sure what the question was, but it did give me the opportunity to play around with filtered indexes and write some simple demo code to illustrate just the kind of shenanigans that you can get up to.

Read more about Filtered Indexes and Dynamic SQL 21 comments — Join the discussion
T-SQL & Development

What Developers Need to Know About SQL Server

What Developers Need to Know About Designing Databases
https://twitter.com/SQLintheWild/status/393386854000492545

One of my favorite tips, and I never thought of it that way. Code is agile, and databases are brittle. It seems easy to refactor the database in the beginning, but as your app grows and more stuff interfaces with it, life gets a lot harder.

Read more about What Developers Need to Know About SQL Server 1 comment — Join the discussion
Performance Tuning

Frequently Asked Questions About TempDB

The questions came fast and furious in one of my recent TempDB webcasts, so here's the ones I wasn't able to answer during the live session:

Q: Virtualized OS, SAN, no dedicated LUNs, most likely scenario with no gotchas, theoretically: dump TempDB with everything else all on one virtual volume, including logs -- or split to separate virtual volumes and separate from other databases and also from logs? (Matthew N.)

Read more about Frequently Asked Questions About TempDB 42 comments — Join the discussion
Performance Tuning

What is the CXPACKET Wait Type, and How Do You Reduce It?

When you query sys.dm_os_wait_stats, or check your waits with sp_Blitz® or sp_BlitzFirst®, one of your biggest wait types is probably CXPACKET. Let's talk about what's really going on. Our SQL Server Stand-In: a Class. Pretend that you're a schoolteacher in a classroom with a couple dozen students. You've got a class project that you need to…

Read more about What is the CXPACKET Wait Type, and How Do You Reduce It? 122 comments — Join the discussion
Performance Tuning

Trivial Optimization and Missing Indexes in SQL Server

Recently, Jes asked the team an index tuning question: "If a query has an index hint in it, will the optimizer ever suggest a missing index for that query?"

I immediately loved the question because I'd never really thought about it before. I typically think of index hints as being a very risky game and avoid them whenever I can-- after all if someone drops the index you've hinted, any query hinting a non-existent index will start to fail. (That's a really bad day!)

Read more about Trivial Optimization and Missing Indexes in SQL Server 7 comments — Join the discussion
Performance Tuning

Optimize for… Mediocre?

Some query hints sound too good to be true. And, unfortunately, usually they aren't quite as magical as they might seem.
Frustration with unpredictable execution times
People often learn about parameter sniffing when query execution times stop being predictable. Occasionally you'll hear about a stored procedure taking much longer than normal, but the next time you look, it might be faster.

Read more about Optimize for… Mediocre? 36 comments — Join the discussion
Performance Tuning

The Index Your Queries will Never Request (A Clustered Index!)

When it comes to indexes, SQL Server is really helpful. It lets you see what indexes queries are asking for both in execution plans, and missing index dynamic management views ("DMVs"). I like to look at the DMV missing index requests using sp_BlitzIndex®.

When you look at missing index requests, it's always important to remember one of the biggest things: these missing index requests won't ever ask for or recommend a specific clustered index.

Read more about The Index Your Queries will Never Request (A Clustered Index!) 3 comments — Join the discussion

SQL Server Management Studio: “Include Client Statistics” Button

I’m curious. I like to know how things work. I have to read the user manual of everything I buy so I know all of its features. I like clicking all the buttons in applications I work with. However, SQL Server Management Studio (SSMS) has so many I haven’t gotten through all of them. At SQL Saturday #118 Wisconsin earlier this year, I was watching a presentation and the presenter clicked “Include Client Statistics”.

Read more about SQL Server Management Studio: “Include Client Statistics” Button 14 comments — Join the discussion
Performance Tuning

Introducing the SQL Server Plan Cache (and a Better sp_Blitz®)

When you send your beautifully hand-crafted organic T-SQL statement to SQL Server, the database engine takes a moment to appreciate the poetry of your work.  Before it turns your request into results, it has to build an execution plan.  SQL Server considers: Which tables it needs to join together What subqueries it needs to execute…

Read more about Introducing the SQL Server Plan Cache (and a Better sp_Blitz®) 22 comments — Join the discussion

The Use and Abuse of RECOMPILE in SQL Server (Video)

What are the costs when you ask SQL Server to recompile a statement or a stored procedure each time it runs? In this 30 minute talk Kendra Little explains the impact on query execution-- and on your SQL Server instance-- of forcing recompilation. This talk is appropriate for DBAs and Developers who have a working knowledge of execution plans in SQL Server.

Read more about The Use and Abuse of RECOMPILE in SQL Server (Video) 8 comments — Join the discussion

How Do You Mask Data for Secure Testing?

Data masking, data scrambling, and just plain old obfuscation: these are ways to get developers access to production-quality data for testing purposes without actually giving them real production data.  It's much harder than it looks, though. The Easy Part: Obfuscating Data Developers love working with production data.  Today's privacy-concerned companies aren't quite so keen on…

Read more about How Do You Mask Data for Secure Testing? 23 comments — Join the discussion