T-SQL and Database Development

Writing, reviewing, debugging, and maintaining T-SQL and database code.

478 associated posts232 primary posts

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-6a6f0859b27ff460372354/] In the…

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

Query Exercise Answer: Fixing a Slow Computed Column

In last week's Query Exercise, we added a user-defined function to the Users table to check whether their WebsiteUrl was valid or not. I noted that even with an index on Reputation, SQL Server 2022 simply ignored the index, did a table scan, and spent 2 minutes of time calling the user-defined function on a row-by-row basis.

Read more about Query Exercise Answer: Fixing a Slow Computed Column 5 comments — Join the discussion
T-SQL & Development

Does Bit Column Order Matter in Tables?

At the PGConf.dev, where Postgres developers get together and strategize the work they wanna do for the next version, I attended a session where Matthias van de Meent talked about changing the way Postgres stores columns. As of right now (Postgres 17), columns are aligned in 8-bit intervals, so if you create a table with alternating columns:

Read more about Does Bit Column Order Matter in Tables? 3 comments — Join the discussion
Performance Tuning

Query Exercise Answer: Beating ChatGPT at Finding Good Question Times

For this week's Query Exercise, I asked you to write a better query than ChatGPT wrote. Your goal was to find the best days and times to post questions on Stack Overflow.

I found it interesting that a lot of the initial answers focused on the times when there were the most questions, or which questions were the most highly upvoted. For me, the best time to post a question is when you have the highest likelihood of getting the right answer, quickly.

Read more about Query Exercise Answer: Beating ChatGPT at Finding Good Question Times 2 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

Who’s Changing the Table? Answers and Discussion

Your challenge for this week was to find out who keeps mangling the contents of the AboutMe column in the Stack Overflow database.

Conceptually, there are a lot of ways we can track when data changes: Change Tracking, Change Data Capture, temporal tables, auditing, and I'm sure I'm missing more. But for me, there are a couple of key concerns when we need to track specific changes in a high-throughput environment:

Read more about Who’s Changing the Table? Answers and Discussion 12 comments — Join the discussion

[Video] Office Hours: Ask Me Anything About Azure and Microsoft Databases

Back at home in the office, time to settle in with a nice caffeine-free Diet Coke and go through your top-voted questions from https://pollgab.com/room/brento. Why caffeine-free? Because I slug multiple coffees first thing in the morning when I wake up (usually around 3am-4am), and by the time I stream with y'all, I don't need any more go juice.

Read more about [Video] Office Hours: Ask Me Anything About Azure and Microsoft Databases 4 comments — Join the discussion
Performance Tuning

Finding Sister Locations to Help Each Other: Answers & Discussion

This week's query exercise asked you to find two kinds of locations in the Stack Overflow database:

Locations populated with users who seem to be really helpful, meaning, they write really good answers
Locations where people seem to need the most help, meaning, they ask a lot of questions, but they do not seem to be answering those of their neighbors

Read more about Finding Sister Locations to Help Each Other: Answers & Discussion 2 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-6a6f0859bf551840129495/]

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

Find Recent Superstars: Answers & Discussion

Your query exercise for this week was to write a query to find users created in the last 90 days, with a reputation higher than 50 points, from highest reputation to lowest. Because everyone's Stack Overflow database might be slightly different, we had to start by finding the "end date" for our query. I'm working with the 2018-06 export that I use in my training classes, so here's my end date:

Read more about Find Recent Superstars: Answers & Discussion 1 comment — Join the discussion