Category: T-SQL

T-SQL & Development

T-SQL Tuesday Roundup: You Know A Query’s Gonna Be Bad When…

For this month's T-SQL Tuesday, I invited y'all to tell me how you know a query's gonna be bad when you open it. Riccardo Perico and Shane O'Neill says WHILE loops and/or CURSORs are pretty good indicators. I agree, because especially when you're opening code in the year 2026, and someone still hasn't figured this out…

Read more about T-SQL Tuesday Roundup: You Know A Query’s Gonna Be Bad When… 11 comments — Join the discussion
T-SQL & Development

Get Your Blog Posts Ready for T-SQL Tuesday #200. Here’s the Topic.

Way back in December of 2009, Adam Machanic published a blog post (archive) in which he invited database bloggers to participate in a new monthly event he called T-SQL Tuesday. Each month, he (or a blogger he picked) proposed a topic, and on that date, we'd all publish our blog posts at the same time. It was a fun way to get a variety of opinions about a topic.

Read more about Get Your Blog Posts Ready for T-SQL Tuesday #200. Here’s the Topic. 53 comments — Join the discussion
T-SQL & Development

Update: SQL Server 2025’s REGEX Performance Isn’t So Bad!

Back in March 2025 when Microsoft first announced that REGEX support was coming to SQL Server 2025 and Azure SQL DB, I gave it a quick test, and the performance was horrific. It was bad in 3 different ways:

The CPU usage was terrible, burning 60 seconds of CPU time to check a few million rows
It refused to use an index
The cardinality estimation was terrible, hard-coded to 30% of the table

Read more about Update: SQL Server 2025’s REGEX Performance Isn’t So Bad! 14 comments — Join the discussion
T-SQL & Development

The Query Tuning Trick You Should Use More: Pagination

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 the query's actually returning a reasonable number of rows out in the wild.

Read more about The Query Tuning Trick You Should Use More: Pagination 6 comments — Join the discussion
T-SQL & Development

T-SQL Has Regex in SQL Server 2025. Don’t Get Too Excited.

Regular expressions are a way of doing complex string searches. They can be really useful, but they have a reputation: they're hard to write, hard to read, and they're even harder to troubleshoot. Once you master 'em, though, they come in handy for very specific situations.

This post isn't about their complexity, though. This post is about Azure SQL DB & SQL Server 2025's regex performance.

Read more about T-SQL Has Regex in SQL Server 2025. Don’t Get Too Excited. 23 comments — Join the discussion
Performance Tuning

Query Exercise: Looking for Email Addresses

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."

Read more about Query Exercise: Looking for Email Addresses 12 comments — Join the discussion
Performance Tuning

Query Exercise: Why Are These 3 Estimates So Wrong?

Our prior Query Exercise introduced SQL Server's 201 buckets problem: its inability to accurately estimate rows for more than 201 outliers in a table. I followed up with a solution using filtered statistics to help with the next 200 outliers, and I talked about how that's really overkill for the simple problem we were facing in that initial challenge.

Read more about Query Exercise: Why Are These 3 Estimates So Wrong? 6 comments — Join the discussion
Performance Tuning

Query Exercise Answers: Solving the 201 Buckets Problem

In this week's Query Exercise challenge, I explained SQL Server's 201 buckets problem. SQL Server's statistics only handle up to ~201 outliers, which means that outliers ~202-300 get wildly inaccurate estimates.

In our example, I had an index on Location and perfectly accurate statistics, but even still, this query gets bad estimates because Lithuania is in outliers ~202-300:

Read more about Query Exercise Answers: Solving the 201 Buckets Problem 8 comments — Join the discussion
Performance Tuning

Query Exercise: Solving The 201 Buckets Problem

When you run a query, SQL Server needs to estimate the number of matching rows it'll find - so that it can decide which indexes to use, whether to go parallel, how much memory to grant, and more.

For example, take any Stack Overflow database, and let's say I have an index on Location, and I want to find the top-ranking users in Lithuania:

Read more about Query Exercise: Solving The 201 Buckets Problem 14 comments — Join the discussion
Performance Tuning

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

This Query Exercise was very different: I didn't ask you to solve a particular problem. I pointed out that I've heard advice that SELECT MAX is faster than SELECT TOP 1, and that's not quite true. I asked you to find factors that would cause these two queries to get different execution plans: [crayon-6a74e5048b348455295531/] In the…

Read more about Query Exercise Answer: What Makes SELECT TOP 1 or SELECT MAX Different? 4 comments — Join the discussion