Development

Missing order id 2

Identity Columns Can Have Gaps, and That’s Okay.

Development
9 Comments
Say you’ve got a table with an identity column, something that’s supposed to start at 1 and go up to a bajillion: Transact-SQL DROP TABLE IF EXISTS dbo.Orders; CREATE TABLE dbo.Orders (Id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, OrderDate DATETIME2, CustomerName NVARCHAR(50)); GO /* Put a starter row in there: */ INSERT INTO dbo.Orders (OrderDate, CustomerName)…
Read More
Accurate estimates

Functions in the WHERE Clause Are Bad… Right?

Statistics
11 Comments
Nah, not necessarily. SQL Server’s query optimizer behavior keeps changing with every freakin’ version. Let’s illustrate it with a simple query against the Stack Overflow Users table: Transact-SQL CREATE INDEX Location ON dbo.Users(Location); GO SELECT COUNT(*) FROM dbo.Users WHERE Location = N'Ahmadabad, India' OPTION (RECOMPILE); 12345 CREATE INDEX Location ON dbo.Users(Location);GOSELECT COUNT(*) FROM dbo.Users    WHERE Location…
Read More
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
Without compression

Which Should You Use: VARCHAR or NVARCHAR?

Development
29 Comments
You’re building a new table or adding a column, and you wanna know which datatype to use: VARCHAR or NVARCHAR? If you need to store Unicode data, the choice is made for you: NVARCHAR says it’s gonna be me. But if you’re not sure, maybe you think, “I should use VARCHAR because it takes half…
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
Mastering Query Tuning

Mastering Class Updates on Resource_Semaphore, The Ascending Key Problem, and The Cloud

Development
2 Comments
I’ve posted a couple of updated videos to my Mastering courses. In Mastering Server Tuning, I updated and expanded the RESOURCE_SEMAPHORE module to cover more details about query workspace grants, plus cover SQL Server 2022’s updates to Adaptive Memory Grants. In Mastering Query Tuning, I added a new module on the Ascending Key Problem. It’s…
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