Category: Query Exercises

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: Beat ChatGPT at Finding Good Question Times

What are the best days of the week and times of the day to post a question at StackOverflow.com? It seems like a simple question, but it's surprisingly nuanced. I asked ChatGPT's latest version, 4o, and its answer made me laugh out loud. First off, the T-SQL is terrible: it creates a completely unnecessary temp…

Read more about Query Exercise: Beat ChatGPT at Finding Good Question Times 14 comments — Join the discussion
Performance Tuning

Query Exercise: Finding Sister Locations to Help Each Other

For this week's query exercise, let's start with a brief query to get a quick preview of what we're dealing with:
[crayon-6a6f0c78ac6c7038262930/]

That query has a few problems, but hold that thought for a moment. (You're going to have to solve those problems, but I just wanted to show you the sample data at first to give you a rough idea of what we're dealing with.)

Read more about Query Exercise: Finding Sister Locations to Help Each Other 12 comments — Join the discussion
Performance Tuning

Query Exercise: Finding Long Values Faster

Our developers have come to us with a problem query that isn't as fast as they'd like. Using any Stack Overflow database:
[crayon-6a6f0c78ad0b7607699152/]
It has an index, but SQL Server refuses to use that index in the execution plan:

If we force the index with a query hint, we do indeed get dramatically lower logical reads. In my particular database's case, the clustered index scan is 141,573 logical reads - but scanning the DisplayName index alone is just 38,641 logical reads.

Read more about Query Exercise: Finding Long Values Faster 45 comments — Join the discussion
Performance Tuning

Query Exercise: Improving Cardinality Estimation

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:
[crayon-6a6f0c78ad824152913853/]
You can test it with any version of the Stack Overflow database. To test it, we'll turn on a couple of tuning options:
[crayon-6a6f0c78ad829977676637/]
The actual execution plan does use our index - not just once, but twice:

Read more about Query Exercise: Improving Cardinality Estimation 33 comments — Join the discussion
Performance Tuning

Query Exercise: Find the Best Time for Maintenance

If we've gotta take the database down for maintenance - perhaps a version upgrade, perhaps upgrading our own code, maybe scaling up the hardware - when's the best time to do it?

For this week's query exercise, the business has asked us to identify 3 1-hour periods with the least user activity, in this format:

Read more about Query Exercise: Find the Best Time for Maintenance 46 comments — Join the discussion
Performance Tuning

Query Exercise: Find Posts with the Wrong CommentCount

For 2024, I'm trying something new: weekly homework challenges! For this week's query challenge, we're going to do some data validation. Pop open the Stack Overflow database and check out the CommentCount column on the Posts table: In theory, that's a fast way to check how many Comments a particular Post has, without querying the…

Read more about Query Exercise: Find Posts with the Wrong CommentCount 31 comments — Join the discussion