Category: T-SQL

Performance Tuning

Database Changes to Find Tagged Questions Faster: Answers and Discussion

In the last Query Exercise discussion, we hit a performance wall when we were trying to quickly find questions with a specific tag. We threw up our hands and said it's time to make database changes, but we had a few restrictions: We have a lot of existing code that uses the Posts.Tags column That…

Read more about Database Changes to Find Tagged Questions Faster: Answers and Discussion 5 comments — Join the discussion
Performance Tuning

Finding Tagged Questions Faster: Answers & Discussion

Your query exercise was to take this Stack Overflow query to find the top-voted questions for any given tag:
[crayon-6a7b46c6ef1ce390375048/]
That's currently using this index in its execution plan:
[crayon-6a7b46c6ef1dc928190099/]
And answer 3 questions:

What kinds of tags will perform worse than others for this query?
Could you change the query to perform better?
Could you change the indexes to perform better, without changing the table structure?

Read more about Finding Tagged Questions Faster: Answers & Discussion 11 comments — Join the discussion
Performance Tuning

Finding the Best Time for Maintenance: Answers & Discussion

Your Query Exercise was to find the best time to do database maintenance by querying the Users table, looking for a one-hour window with the lowest number of created users. We kept this one pretty simple by looking at the data in just one table, and we didn't hassle with time zones. We just wanted the 3 lowest-load hours, in this format:

Read more about Finding the Best Time for Maintenance: Answers & Discussion 3 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

Find Posts with the Wrong CommentCount: Answers & Discussion

Your Query Exercise was to find denormalization accuracy problems: checking the accuracy of a reporting column, Posts.CommentCount. There were two parts: finding the top 100 most problematic Posts with the biggest variances, and thinking about a long term solution to keep the CommentCount accuracy as high as practical. Question 1: Finding the Problematic Posts Your…

Read more about Find Posts with the Wrong CommentCount: Answers & Discussion 4 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
T-SQL & Development

FOR XML PATH Changed The Way I Think About T-SQL #TSQL2sday

The first time I saw FOR XML PATH being used to generate a comma-delimited list, I think I stared at it, shook my head to clear the cobwebs, stared at it some more, and then closed the code editor thinking it was complete witchcraft.

And that same thing probably happened the next several times, too.

Read more about FOR XML PATH Changed The Way I Think About T-SQL #TSQL2sday 4 comments — Join the discussion
T-SQL & Development

Can You Nest Transactions in SQL Server?

To find out, let's set up a simple status log table:
[crayon-6a7b46c6f2cad131438194/]
And then let's try a two-part transaction:
[crayon-6a7b46c6f2cb5509247957/]
Right now, SQL Server shows that I have 2 open transactions:

What Happens If I Roll Back?
But what does "2 open transactions" mean, really? If I do a rollback, what gets rolled back? Let's find out:
[crayon-6a7b46c6f2cb9575717125/]
The results:

Read more about Can You Nest Transactions in SQL Server? 5 comments — Join the discussion
T-SQL & Development

Find 40 Problems in This Stored Procedure.

Aaron Bertrand posted a challenge:
We’re going to use the AdventureWorks sample database (get your copy here), where the folks in marketing requested a list of users to e-mail a new promotional campaign. The customers need to meet at least one of the following criteria:

last placed an order more than a year ago
placed 3 or more orders in the past year
have ordered from a specific category in the past two weeks

Read more about Find 40 Problems in This Stored Procedure. Be the first to comment
T-SQL & Development

Should You Use SQL Server 2022’s STRING_SPLIT?

SQL Server 2022 improved the STRING_SPLIT function so that it can now return lists that are guaranteed to be in order. However, that's the only thing they improved - there's still a critical performance problem with it. Let's take the Stack Overflow database, Users table, put in an index on Location, and then test a…

Read more about Should You Use SQL Server 2022’s STRING_SPLIT? 2 comments — Join the discussion
T-SQL & Development

Should You Use SQL Server 2022’s DATETRUNC?

SQL Server 2022 introduced a new T-SQL element, DATETRUNC, that truncates parts of dates. For example:
[crayon-6a7b46c7017dd947429847/]
Truncates everything in that date other than the year, so it returns just 2017-01-01 00:00:

You might ask, "Well, why not just use YEAR()?" That's a good question - there are times when you need a start or end date for a date range, and this could make it easier than trying to construct a full start & end date yourself.

Read more about Should You Use SQL Server 2022’s DATETRUNC? 10 comments — Join the discussion
T-SQL & Development

#tsql2sday: Start Your Dynamic SQL with a Comment.

When you write dynamic SQL, start like this:
[crayon-6a7b46c702008074842331/]
Right after the SELECT (or INSERT or UPDATE or whatever), immediately put a comment - using /*, of course, because you're not a terrible person.

That way, when you're looking at the plan cache or monitoring tools, you can see what generated the dynamic SQL, and where you need to go if you need to performance tune or fix it.

Read more about #tsql2sday: Start Your Dynamic SQL with a Comment. 6 comments — Join the discussion
T-SQL & Development

[Video] Fundamentals of Stored Procedures at SQLBits

Anybody can write a stored procedure with a little help from Google. This session is about how to write stored procedures that have a high likelihood of performing well and are easy to troubleshoot.

This fast-paced, all-demo session from SQLBits will NOT cover how to write a query, syntax, or performance tuning. This is about good best practices after you've written the first one - things like how to catch errors, how to pass in multiple values, how to debug without the debugger, and more.

Read more about [Video] Fundamentals of Stored Procedures at SQLBits 12 comments — Join the discussion