Fundamentals of TempDB

T-SQL Query Exercise: Generate Big Spills

Query Exercises
18 Comments
When I was testing SQL Server 2025’s new ability to limit TempDB usage with Resource Governor, I wrote a few fun diabolical demos. One of them was to generate giant spills to TempDB, quickly. When you’re looking at an actual (not estimated) query plan, and you see a yellow exclamation point on a sort operator,…
Read More
it hurts so good, I don't understand

The Query Tuning Trick You Should Use More: Pagination

T-SQL
6 Comments
When I’m tuning queries, the normal answer is to make the query perform better – either via changing the T-SQL, adding hints, or adding indexes so that the data’s better prepared for the query. However, sometimes when I’m looking at the output of sp_BlitzCache, I scroll across to the Average Rows column and double-check that…
Read More
The Dude Abides

Query Exercise Answers: Returning Routes in the Right Order

In your most recent Query Exercise challenge, I gave you these two tables: Transact-SQL DROP TABLE IF EXISTS dbo.Stations; CREATE TABLE dbo.Stations (StationId INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, StationName VARCHAR(50), StationPhysicalOrder INT); INSERT INTO dbo.Stations (StationName, StationPhysicalOrder) SELECT CHAR(64 + n), n FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11), (12),(13),(14),(15),(16),(17),(18),(19) ) AS Numbers(n); DROP TABLE IF EXISTS dbo.StationRoutingOverride; CREATE…
Read More

Query Exercise: Looking for Email Addresses

Query Exercises
12 Comments
“They put their password WHERE?!?” Personally identifiable information (PII) is freakin’ everywhere. When companies first start looking to identify and lock down their data, they think it’s going to be as easy as identifying common columns like EmailAddress, DateOfBirth, SocialSecurityNumber, and so forth. They think, “We’ll just encrypt those columns and we’ll be fine.” Ho…
Read More

Should That Be One Update Statement or Multiple?

T-SQL
12 Comments
Let’s say we have a couple of update statements we need to run every 15 minutes in the Stack Overflow database, and we’ve built indexes to support them: Transact-SQL EXEC DropIndexes @TableName = N'Users'; CREATE INDEX LastAccessDate ON dbo.Users(LastAccessDate); CREATE INDEX AccountId ON dbo.Users(AccountId); GO BEGIN TRAN /* Give reputation points if folks did something…
Read More

TRY/CATCH Doesn’t Always Work.

T-SQL
11 Comments
If you’re using TRY/CATCH to do exception handling in T-SQL, you need to be aware that there are a lot of things it doesn’t catch. Here’s a quick example. Let’s set up two tables – bookmarks, and a process log to track whether our stored proc is working or not: Transact-SQL DROP TABLE IF EXISTS dbo.Bookmarks;…
Read More

Query Exercise: What Makes SELECT TOP 1 or SELECT MAX Different?

Query Exercises
10 Comments
These two queries both get the same answer from the Stack Overflow database: Transact-SQL SELECT TOP 1 LastAccessDate FROM dbo.Users ORDER BY LastAccessDate DESC; SELECT MAX(LastAccessDate) FROM dbo.Users; 123456 SELECT TOP 1 LastAccessDate    FROM dbo.Users    ORDER BY LastAccessDate DESC; SELECT MAX(LastAccessDate)    FROM dbo.Users; But do they go about their work the same way? As it turns out, no –…
Read More

Query Exercise: Fix This Computed Column.

Query Exercises
54 Comments
Take any size of the Stack Overflow database and check out the WebsiteUrl column of the Users table: Sometimes it’s null, sometimes it’s an empty string, sometimes it’s populated but the URL isn’t valid. Let’s say that along the way, someone decided to ask ChatGPT to build a function to check for valid website URLs,…
Read More

The SQL Language is Just a Hurdle You Gotta Overcome.

T-SQL
33 Comments
I’m not talking just about Microsoft SQL Server specifically here, nor T-SQL. Let’s zoom out a little and think bigger picture for a second: is the SQL language itself a problem? Sometimes when I talk to client developers, they gripe about the antiquated language. The order of a SELECT statement doesn’t make any sense. You…
Read More