Development

The Last Ticket/Issue I Closed #TSQL2sday

T-SQL
19 Comments
For this month’s T-SQL Tuesday, I asked y’all to write about the most recent ticket or issue that you closed. (If you want to see other peoples’ posts, you can check out the comments on that invite post, or wait til next week and I’ll publish a wrap-up of everyone’s answers.) A past client emailed…
Read More

Query Exercise: Improving Cardinality Estimation

Query Exercises
33 Comments
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: Transact-SQL CREATE INDEX Location ON dbo.Users(Location); GO CREATE OR ALTER PROC dbo.GetTopUsersInTopLocation AS SELECT TOP 200 u.Reputation, u.Id, u.DisplayName, u.WebsiteUrl, u.CreationDate…
Read More

Query Exercise: Find Tagged Questions Faster.

Query Exercises
42 Comments
For this week’s Query Exercise, we’re going to start with an existing query that has performance problems from time to time, depending on how it’s used. This query’s goal is to find the top-voted Stack Overflow questions for any given tag – for example, here are the top-voted questions tagged “sql-server”. What are tags? I’m…
Read More

Find Posts with the Wrong CommentCount: Answers & Discussion

Query Exercise Answers
4 Comments
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

Query Exercise: Find Foreign Key Problems

Query Exercises
21 Comments
For 2024, I’m trying something new: weekly homework challenges! For this week, let’s say we’ve decided to implement foreign keys, and we need to find data that’s going to violate our desired keys. We’re going to use the Stack Overflow database, and we’ll focus on these 3 tables: dbo.Users table: with Id column as its…
Read More

What’s Faster: IN or OR? Columnstore Edition

Pinal Dave recently ignited a storm of controversy when he quizzed readers about which one of these would be faster on AdventureWorks2019: SELECT * FROM Sales.SalesOrderHeader WHERE CustomerID IN (1, 2, 3); SELECT * FROM Sales.SalesOrderHeader WHERE CustomerID = 1 OR CustomerID = 2 OR CustomerID = 3; 123456789 SELECT *FROM Sales.SalesOrderHeaderWHERE CustomerID IN (1,…
Read More

How to Make Database Changes Without Breaking Everything

Development
8 Comments
You’ve got an existing application with a database back end. You’re thinking about changing the database, and you don’t wanna break stuff. The most important thing to understand is difference between constructive and destructive changes, also known as additive and destructive changes, or non-breaking and breaking changes. Constructive change examples: adding a new table or…
Read More

Can You Nest Transactions in SQL Server?

T-SQL
5 Comments
To find out, let’s set up a simple status log table: DROP TABLE IF EXISTS dbo.StatusLog; CREATE TABLE dbo.StatusLog (TimeItHappened DATETIME2 PRIMARY KEY CLUSTERED, Step VARCHAR(20)); GO 123456 DROP TABLE IF EXISTS dbo.StatusLog; CREATE TABLE dbo.StatusLog     (TimeItHappened DATETIME2 PRIMARY KEY CLUSTERED,     Step VARCHAR(20));GO And then let’s try a two-part transaction: BEGIN TRAN INSERT INTO dbo.StatusLog VALUES…
Read More
Brent Reading Book

Find 40 Problems in This Stored Procedure.

T-SQL
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…
Read More

3 Ways to Debug T-SQL Code

T-SQL
41 Comments
Writing new code = bugging. That part’s easy. Taking those bugs back out, that’s the hard part. Developers are used to their tools having built-in ways to show what line of code is running now, output the current content of variables, echo back progress messages, etc. For a while, SQL Server Management Studio also had…
Read More

How to Find Missing Rows in a Table

T-SQL
21 Comments
When someone says, “Find all the rows that have been deleted,” it’s a lot easier when the table has an Id/Identity column. Let’s take the Stack Overflow Users table: It has Ids -1, 1, 2, 3, 4, 5 … but no 6 or 7. (Or 0.) If someone asks you to find all the Ids…
Read More