Category: T-SQL

T-SQL & Development

If You Have Foreign Keys, Don’t Update Fields That Aren’t Changing.

If you update a row without actually changing its contents, does it still hurt?

Paul White wrote in detail about the impact of non-updating updates, proving that SQL Server works hard to avoid doing extra work where it can. That's a great post, and you should read it.

Read more about If You Have Foreign Keys, Don’t Update Fields That Aren’t Changing. 25 comments — Join the discussion
Performance Tuning

How to Get Better Estimates for Modification Queries

When you're doing DUI operations against tables with millions of rows, you have to be really careful about SQL Server's estimates. Ideally, you want your delete/update/insert (what - what'd you think I meant?) queries to be as simple as possible - even a simple join can cause SQL Server to do wildly, wildly incorrect estimates, which affects memory grants, parallelism, wide vs narrow plans, and more.

Read more about How to Get Better Estimates for Modification Queries 4 comments — Join the discussion
Performance Tuning

Why Ordering Isn’t Guaranteed Without an ORDER BY

If your query doesn't have an ORDER BY clause,
you can't reliably predict the order of your results over time.

Sure, it's going to look predictable at first, but down the road, as things change - the indexes, the table, the server's configuration, the size of your data - you can end up with some ugly surprises.

Read more about Why Ordering Isn’t Guaranteed Without an ORDER BY 10 comments — Join the discussion
Performance Tuning

Making TRY_CAST and TRY_CONVERT Queries Faster with Indexed Computed Columns

I know this is gonna sound crazy, but let's say you had a table where people stored all kinds of things in one column: dates, integers, file names, sale prices, file names, you name it. And let's say your application frequently ran a query looking for dates in that column, like this: [crayon-6a79e73ea4e53231306722/] Even if…

Read more about Making TRY_CAST and TRY_CONVERT Queries Faster with Indexed Computed Columns 2 comments — Join the discussion
T-SQL & Development

How to Remove Times from Dates in SQL Server

Say that you've got a table with a datetime column in it, like the LastAccessDate column in the Stack Overflow Users table:

And let's say you wanna find the users who last accessed the site on September 9, 2018 - regardless of the time. Here are a few ways to do it:
[crayon-6a79e73ea65e3611123102/]
Here are their actual execution plans. I'm using SQL Server 2019, with 2019 compatibility level, and the 50GB StackOverflow2013 database.

Read more about How to Remove Times from Dates in SQL Server 34 comments — Join the discussion
T-SQL & Development

[Video] You shouldn’t dynamically create/alter/drop triggers.

Let's get one thing out of the way first: this isn't about the relative good or evil of triggers. I kinda like triggers, in fact - they're good for enforcing business logic in a hurry when you can't redo code across an entire application. Sure, in a perfect world, we'd always have the code do whatever the code needs to do - but there are times when you can't, and triggers come in handy then.

Read more about [Video] You shouldn’t dynamically create/alter/drop triggers. 10 comments — Join the discussion
T-SQL & Development

User-defined scalar functions suck – even when they don’t access data.

The performance of scalar functions sucks hard. Let's see it in action using the Stack Overflow database - any size will work. I'll set things up first in case you want to follow along: [crayon-6a79e73ea8cec160856672/] I'm purposely setting my Cost Threshold for Parallelism to be low here because I want to demonstrate what happens when…

Read more about User-defined scalar functions suck – even when they don’t access data. 22 comments — Join the discussion
T-SQL & Development

The Silent Bug I Find in Most Triggers

I don't have a problem with triggers. They get the job done when you need to implement business logic in a hurry, and you're not allowed to change the application. As long as you keep the number of statements to a minimum (say, 2-3), and don't try to do something really slow like fire up a cursor, triggers can be an efficient way to solve hard problems quickly.

Read more about The Silent Bug I Find in Most Triggers 35 comments — Join the discussion
Performance Tuning

Finding Froid’s Limits: Testing Inlined User-Defined Functions

This week, I've been writing about how SQL Server 2019's bringing a few new features to mitigate parameter sniffing, but they're more complex than they appear at first glance: adaptive memory grants, air_quote_actual plans, and adaptive joins. Today, let's talk about another common cause of wildly varying durations for a single query: user-defined functions.

Read more about Finding Froid’s Limits: Testing Inlined User-Defined Functions 16 comments — Join the discussion
T-SQL & Development

Should we use stored procedures or queries built in the app?

A client asked a great architecture question that deserved its own blog post:
Should we use more "stored procedures" for select, insert, update, delete or should that just be sent as queries from the application?
If you ask a junior database administrator where to put something, she'll probably say "in the database" because that's the thing she has control over. That's fair - DBAs love having control over stuff. However, let's step back a little.

Read more about Should we use stored procedures or queries built in the app? 71 comments — Join the discussion
T-SQL & Development

Tuning Dynamic SQL by Hand with Short Circuits

When we think about building dynamic SQL, we usually think about a stored procedure like this that takes input parameters, builds a string, and then executes that string. Here's a simple example: [crayon-6a79e73eacf11958022747/] The more parameters you have, the crazier the code gets, and the harder it becomes to produce really fast plans across all…

Read more about Tuning Dynamic SQL by Hand with Short Circuits 7 comments — Join the discussion
T-SQL & Development

What Is SQL Injection?

Say we have a stored procedure that queries the Stack Overflow database. We have two separate parameters, @DisplayName and @Location, so folks can search for people by name, location, or both.

For performance reasons, we decide to build dynamic SQL:
[crayon-6a79e73eae146054749919/]
When we run it, it works, and we can see the query on the Messages tab in SSMS:

Read more about What Is SQL Injection? 4 comments — Join the discussion
T-SQL & Development

What’s New in SQL Server 2019: Faster Functions

A while back, we talked you through a public whitepaper about how Microsoft was working on making user-defined functions go faster. Now that the preview of SQL Server 2019 is out, you can start getting your hands on Froid, the performance-boosting feature. Here's the documentation on it - let's see how it works. Using the…

Read more about What’s New in SQL Server 2019: Faster Functions 10 comments — Join the discussion
Performance Tuning

What’s New in SQL Server 2019: Faster Table Variables (And New Parameter Sniffing Issues)

For over a decade, SQL Server's handling of table variables has been legendarily bad. I've long used this Stack Overflow query from Sam Saffron to illustrate terrible cardinality estimation:
[crayon-6a79e73eafd57434506010/]
It puts a bunch of data into a table variable, and then queries that same table variable. On the small StackOverflow2010 database, it takes almost a full minute, and does almost a million logical reads. Here's the plan:

Read more about What’s New in SQL Server 2019: Faster Table Variables (And New Parameter Sniffing Issues) 17 comments — Join the discussion