Author: Erik Darling

Restoring tempdb since GETDATE(). Now blogging at ErikDarlingData.com.
Performance Tuning

Sniffed Nulls and Magic Numbers

I Sniff Your Milkshake
Building off of A Simple Stored Procedure Pattern To Avoid, I wanted to talk about a similar one that I see quite often that is not nearly as clever as one would imagine.

I goes something like this: If this variable is passed in as NULL, substitute it with something else. It has a lot of variations.
[crayon-6aa44c7d74a3e055947893/]
They all have the desired effect: substituting a passed in NULL with a magic number.

Read more about Sniffed Nulls and Magic Numbers 8 comments — Join the discussion
Performance Tuning

Never Judge A Query By Its Cost

Signs and Numbers
When tuning queries, or even finding queries to tune, there's a rather misguided desire to look for queries with a high cost, or judge improvement by lowering query cost. The problem is that no matter what you're looking at, costs are estimates, and often don't reflect how long a query runs for or the actual work involved in processing the query.

Read more about Never Judge A Query By Its Cost 7 comments — Join the discussion
Performance Tuning

Indexed View Creation And Underlying Indexes

Accidental Haha
While working on some demos, I came across sort of funny behavior during indexed view creation and how the indexes you have on the base tables can impact how long it takes to create the index on the view.

Starting off with no indexes, this query runs in about six seconds.
[crayon-6aa44c7d76a4d174480374/]
Here's the plan and the query stats:

Read more about Indexed View Creation And Underlying Indexes 1 comment — Join the discussion
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