Black Friday prices end in 20d 17h 46m — See the sale

Category: T-SQL

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-6af2b5ea2e496911196723/]
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
T-SQL & Development

Stupid T-SQL Tricks

Presented without comment:
[crayon-6af2b5ea30176490893184/]
Next up, can you break up a query with spaces? Yep:
[crayon-6af2b5ea30187831840474/]
Well if you can do that - can you break up a query across lines? Sure you can:
[crayon-6af2b5ea3018b865532961/]
And now, brace yourself: this one is so weird that I can't even embed it in the blog. I'm just going to show you a picture of it first:

Read more about Stupid T-SQL Tricks 33 comments — Join the discussion
T-SQL & Development

Fifteen Things I Hate About ISNUMERIC

Yello!
[crayon-6af2b5ea34073808193860/]
Thanks for reading!

Brent says: the funny part to me is that if you just try to union all of them, SQL Server throws its hands up:
[crayon-6af2b5ea34084949157565/]
Result:
[crayon-6af2b5ea34088248104903/]
COME ON SQL SERVER YOU JUST TOLD ME THEY WERE ALL NUMERIC, dammit, convert to numeric for me if you're so smart.

Read more about Fifteen Things I Hate About ISNUMERIC 22 comments — Join the discussion
T-SQL & Development

Using WITH (NOEXPAND) to Get Parallelism with Scalar UDFs in Indexed Views

Scalar functions are the butt of everybody's jokes: their costs are wrong, their STATS IO results are wrong, they stop parallelism when they're in check constraints, their stats are wrong in 2017 CU3, they stop parallelism in index rebuilds and CHECKDB, I could go on and on.

Recently, we ran across yet another scenario where scalar UDFs were killing performance.

Read more about Using WITH (NOEXPAND) to Get Parallelism with Scalar UDFs in Indexed Views 7 comments — Join the discussion
T-SQL & Development

Creating Insert Triggers to Silently Ignore Data You Don’t Want

Say you've got an application that insists on inserting data into the database, and...you don't want the data.

You want the application to THINK it inserted the data - you don't want to roll it back or return an error to the end user. You just don't want the data, and you don't want the hassle of deleting it later.

Read more about Creating Insert Triggers to Silently Ignore Data You Don’t Want 19 comments — Join the discussion
T-SQL & Development

Using LIKE on Integers Gets You Implicit Conversion

Using the Stack Overflow public export, take these two queries looking for a particular user by Id (the clustering key):
[crayon-6af2b5ea37da9181459058/]
The first one (=) gets a clustered index seek, does just 3 logical reads, and correctly estimates that only 1 row will be returned.

The second one (LIKE) does a clustered index scan, reads the entire table, and wildly overestimates that 475,005 rows will be returned - even though only 1 row will.

Read more about Using LIKE on Integers Gets You Implicit Conversion 5 comments — Join the discussion
T-SQL & Development

Don’t Use Scalar Functions in Views.

The short story: if your view has a scalar user-defined function it it, any query that calls the view will go single-threaded, even if the query doesn't reference the scalar function. Now for the long story.

Quite often people will inherit and rely on views written back in the dark ages, before people were aware of the deleterious effects that scalar valued functions can have on performance.

Read more about Don’t Use Scalar Functions in Views. 3 comments — Join the discussion
T-SQL & Development

No seriously, don’t use SQL_VARIANT for that

I'd been meaning to write this for a while I half-stumbled on the weirdness around SQL_VARIANT a while back while writing another post about implicit conversion. What I didn't get into at the time is that it can give you incorrect results. When I see people using SQL_VARIANT, it's often in dynamic SQL, when they don't…

Read more about No seriously, don’t use SQL_VARIANT for that 13 comments — Join the discussion