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

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

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

Query Exercise: Finding Sister Locations to Help Each Other

Query Exercises
12 Comments
For this week’s query exercise, let’s start with a brief query to get a quick preview of what we’re dealing with: Transact-SQL SELECT TOP 100 Location, COUNT(*) AS Population, AVG(Reputation) AS AvgReputation FROM dbo.Users GROUP BY Location ORDER BY COUNT(*) DESC; 123456 SELECT TOP 100 Location, COUNT(*) AS Population, AVG(Reputation) AS AvgReputationFROM dbo.UsersGROUP BY LocationORDER…
Read More

Query Exercise: Find Recent Superstars

Query Exercises
44 Comments
For this week’s Query Exercise, we’re working with the Stack Overflow database, and our business users have asked us to find the new superstars. They’re looking for the top 1000 users who were created in the last 90 days, who have a reputation higher than 50 points, from highest reputation to lowest. In your Stack…
Read More

Query Exercise: Finding Long Values Faster

Query Exercises
45 Comments
Our developers have come to us with a problem query that isn’t as fast as they’d like. Using any Stack Overflow database: Transact-SQL CREATE INDEX DisplayName ON dbo.Users(DisplayName); GO SELECT * FROM dbo.Users WHERE LEN(DisplayName) > 35; 12345 CREATE INDEX DisplayName ON dbo.Users(DisplayName);GOSELECT *  FROM dbo.Users  WHERE LEN(DisplayName) > 35; It has an index, but SQL Server…
Read More

Query Exercise: Improving Cardinality Estimation

Query Exercises
33 Comments
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: Transact-SQL CREATE INDEX Location ON dbo.Users(Location); GO CREATE OR ALTER PROC dbo.GetTopUsersInTopLocation AS SELECT TOP 200 u.Reputation, u.Id, u.DisplayName, u.WebsiteUrl, u.CreationDate…
Read More

Query Exercise: Find Tagged Questions Faster.

Query Exercises
42 Comments
For this week’s Query Exercise, we’re going to start with an existing query that has performance problems from time to time, depending on how it’s used. This query’s goal is to find the top-voted Stack Overflow questions for any given tag – for example, here are the top-voted questions tagged “sql-server”. What are tags? I’m…
Read More

Query Exercise: Find Foreign Key Problems

Query Exercises
21 Comments
For 2024, I’m trying something new: weekly homework challenges! For this week, let’s say we’ve decided to implement foreign keys, and we need to find data that’s going to violate our desired keys. We’re going to use the Stack Overflow database, and we’ll focus on these 3 tables: dbo.Users table: with Id column as its…
Read More