Category: Execution Plans

Performance Tuning

Adventures In Foreign Keys 5: How Join Elimination Makes Queries Faster

FINALLY...
This is the last post I'll write about foreign keys for a while. Maybe ever.

Let's face it, most developers probably find them more annoying than useful, and if you didn't implement them when you first started designing your database, you're not likely to go back and start trying to add them in.

Read more about Adventures In Foreign Keys 5: How Join Elimination Makes Queries Faster 14 comments — Join the discussion
Performance Tuning

Adventures In Foreign Keys 4: How to Index Foreign Keys

This week, we're all about foreign keys. So far, we set up the Stack Overflow database to get ready, then tried to set up relationships, and encountered cascading locking issues.
Dawn Of The Data
I don't know how long the recommendation to index your foreign keys has been a thing, but I generally find it useful to abide by, depending a bit on how they're used.

Read more about Adventures In Foreign Keys 4: How to Index Foreign Keys 6 comments — Join the discussion
Performance Tuning

Adventures In Foreign Keys 2: Common Foreign Key Errors

This week, we're all about foreign keys. Yesterday's post covered scripts to set up the Stack Overflow database to get ready, eliminating data that would violate FK relationships.
You Had Two Jobs!
Let's say I wanted to implement Foreign Keys to do two things

If a user deletes their account, all of their badges, comments, and posts will also get deleted
If a user deletes their post, all of the comments and votes will also get deleted

Read more about Adventures In Foreign Keys 2: Common Foreign Key Errors 4 comments — Join the discussion
Performance Tuning

Adventures In Foreign Keys 1: Setting Up Foreign Keys in Stack Overflow

In A Foreign Key, In A Foreign Table Much of what people want from foreign keys, like referential integrity and join elimination, are only as guaranteed as much as SQL Server can trust your constraints (and even then...). The same goes for check constraints, too. Thankfully, things like Primary Keys and Unique Constraints are sort…

Read more about Adventures In Foreign Keys 1: Setting Up Foreign Keys in Stack Overflow 11 comments — Join the discussion
Performance Tuning

How Check Constraints MIGHT Improve Your Queries and Missing Index Requests

The more SQL Server knows about your data, the better your query plans can get.

Say you've got an app that's designed to store multiple companies in a single database - but you don't actually use it that way. All of the data in a given database is actually for the same company.

Read more about How Check Constraints MIGHT Improve Your Queries and Missing Index Requests 14 comments — Join the discussion
Performance Tuning

Filtered Indexes vs Parameterization (Again)

At First I Was Like... This won't work at all, because parameters are a known enemy of filtered indexes. But it turns out that some filtered indexes can be used for parameterized queries, whether they're from stored procedures, dynamic SQL, or in databases with forced parameterization enabled. Unfortunately, it seems limited to filtering out NULLs…

Read more about Filtered Indexes vs Parameterization (Again) 2 comments — Join the discussion
Performance Tuning

Is Cost Threshold for Parallelism Measured in Seconds?

SQL Server automatically chooses when to divide your query's work across multiple CPU cores. It makes that decision based on your query's cost. To see it, let's throw 1,000,000 tiny rows in a table: [crayon-6a6ed259d4eb5719383400/] And with my server set at SQL Server's default settings (Cost Threshold of 5, MAXDOP of 0), count how many…

Read more about Is Cost Threshold for Parallelism Measured in Seconds? 2 comments — Join the discussion
Performance Tuning

SQL Server Management Studio 18’s Execution Plans Will Change The Way You Look At Plan Tuning

The preview of the next version of SQL Server Management Studio is out, and it has a radical improvement to query plans that will shock and amaze you. It's best to just show you: You can see the estimated and actual number of rows right there on the query plan just like live query plans!…

Read more about SQL Server Management Studio 18’s Execution Plans Will Change The Way You Look At Plan Tuning 9 comments — Join the discussion
Performance Tuning

When You Need to Tune A View, Don’t Just Get Its Plan

Say your database has a view, and everybody's queries use it. Let's take the Stack Overflow database and create this view:
[crayon-6a6ed259d6248717502334/]
Not pretty, but that's why you're here, right? Real world code is ugly.

And say my users are running queries like these:
[crayon-6a6ed259d624d359839126/]
Notice that the select, where, order by etc all have variations in them. They're not all asking for the same fields, which means SQL Server can make different decisions about:

Read more about When You Need to Tune A View, Don’t Just Get Its Plan 2 comments — Join the discussion