Development

How you doin

How Multi-Column Statistics Work

Statistics
4 Comments
The short answer: in the real world, only the first column works. When SQL Server needs data about the second column, it builds its own stats on that column instead (assuming they don’t already exist), and uses those two statistics together – but they’re not really correlated. For the longer answer, let’s take a large…
Read More

Should There Be Ads in SSMS?

Some folks are seeing an ad at the top of their SSMS v22, like this one reported in the feedback site: Today, Microsoft’s using this to test ads for an upcoming conference. Interesting that they give deeper discounts to Reddit readers as opposed to SSMS users, but I digress. Tomorrow, they might be pushing SQL…
Read More
Missing order id 2

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

Development
13 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