Categories
SQL

Understanding Expression Evaluation with ISNULL and COALESCE

When eliminating NULL values with SQL Server queries we typically reach for ISNULL or COALESCE to do the job. Generally speaking they’ll provide equivalent results, but they work in different ways. If you’re dealing with logic more complex than ISNULL(ColA, ColB) – for example using a function or subquery as part of the expression – then you might be in for a surprise. […]

Categories
SQL

Why Do We Still Use RAISERROR?

I don’t use RAISERROR often – I usually forget which severity code to use. After looking at a sprinkling of them recently I decided it was time for a refresher, so come along for the ride. If you check out the online documentation it states that “New applications should use THROW instead”. It also sounds like its used to raise […]

Categories
SQL

How Many Rows Are In That Table?

A question I ask myself often when exploring unfamiliar data sets. So here’s a quickie: If you’re in need of a quick measuring stick, this should do the job. Microsoft does note that the row_count field is an approximate value in this DMV. It’s taken from the internal table PARTITIONCOUNTS which can be cached, but […]

Categories
SQL

Batching Large Data Changes Using Key Ranges

Handling data modifications to large tables can be tricky. We can end up with long running queries and large transactions. Efficiently handling them comes from batching. Effective batching in general helps us by: In this post we’ll take a look at a key-range approach to solve the issue. With this solution a batch will be […]

Categories
SQL Server

Discovering More About Join Operator Internals

In the previous post I explained Join Operators in SQL Server. Whilst compiling that I dug a little deeper and came across a few interesting points I thought were worth sharing. Let’s look at behaviour of the operators which may occur under specific conditions. Hopefully you find them as interesting as I did: Optimised nested loop […]

Categories
SQL Server

SQL Server Join Operators Explained

When reviewing our execution plans we’ll see joins executed using different operators. The type of operator is chosen based on the data that’s available to join and how the optimiser wants to execute it. In this post we’ll take a look at what the operators are, when they are used, and how they work. These […]

Categories
SSIS

Solving Bottlenecks in SSIS Packages

Last time out we started to look at optimising SSIS packages by showing how to identify bottlenecks with a handy script. This time we’re turning insights into action to solve those pain points. The solutions are grouped into 3 areas: Data Flows, as they do a lot of heavy lifting; the Execute SQL task, which can […]

Categories
SQL

Every Stored Procedure Should Start with a Header

Code is an ever moving target. Version control and documentation only go so far, if they even exist. Sometimes all you have is the code in front of you. This is why I always start stored procedures with a header. Here’s a template as a starter: Name seems redundant as we have the proc name below […]

Categories
SQL

Data Segmentation in SQL using Window Functions

Sometimes you want to segment records. It may be splitting a customer base for marketing purposes, or segmenting a user base for a new feature. Good segmentation makes clean divisions in the data. In this post we’ll see a way to achieve that with a great deal of help from Window Functions. This post was […]

Categories
SQL

Repercussions of Implicit Conversion

Implicit conversion happens in SQL Server when the engine detects a mismatch in data types and automatically converts from one type to another. This can be helpful as it makes different types interchangeable and is generally transparent to the client, but it can come with issues. Usually the downside from implicit conversion is seen through […]