Press "Enter" to skip to content

Category: T-SQL

When All Joins are Left

Hugo Kornelis gives an example of a red flag:

This anniversary edition is hosted by Brent Ozar. And his chose topic is: query red flags. Things that make you groan when you open a query and see them in the code. I’m sure there will be a ton of posts, because there are so many. I myself could probably fill a book with things I consider a red flag (and someone else would then point out that my queries have things that they consider red flags, but that is another discussion).

But let’s focus on just one thing in this post.

Hugo selects the case when all query joins are LEFT OUTER joins. Especially when the logic of the query mandates INNER joins.

Meanwhile, if all of your query joins are RIGHT OUTER joins, you’re just chaotic evil.

Leave a Comment

Red Flags in Database Code

Tom Zika has a list and starts with AI-generated code:

This one didn’t exist three years ago. Now it’s the first thing I look for.

To be clear, I’m not anti-AI. If the AI wrote clean code, I probably wouldn’t even notice. The red flag isn’t that AI generated it – it’s the patterns that give it away. I recently saw a real case where someone needed to update a set of values. Simple enough, right? Here’s what the AI-generated solution did:

Click through for a laugh, as well as several other red flags.

Leave a Comment

The Siren Song of Code Reusability in SQL Server

Reitse Eskens hits on a long-standing pain point in SQL Server:

In an effort to make code reusable, someone decided to use Scalar User-Defined Functions. About ten of them. And each function was called in the SELECT list of each query sent to the database.

The worst part is, the instincts behind doing something like this are perfectly reasonable. In most programming languages, composing functions or refactoring code into isolated functions that you call is so cheap as to be (almost) free. But in T-SQL, that is rarely the case.

Leave a Comment

SQL Formatting in SSMS 22.7

Chad Callihan tries out a new feature:

Code formatting can be a touchy subject. Sometimes there are clear rules to designate right and wrong, and sometimes there’s not. Tabs versus spaces, anyone?

Surprisingly, SQL Server Management Studio has never had a built-in SQL formatter. Users were always left to use third-party tools or format by hand. But with the latest SSMS 22.7, SQL formatting finally comes built-in.

Let’s look at some examples and see how it performs.

Chad also spotted a problem in the formatter as it is in that release.

Leave a Comment

What “Filtering Early” Really Means

Louis Davidson lays out the facts:

Which brings me to the point. There is a myth that goes around that you need to place filters in your SQL statements as early in the statement as possible. Most of this is due to the wild misunderstanding of how a query is executed (versus how your query is processed, which I covered last week.) The actual issue here is that the concept of filtering early is actually true, but certainly not in the way it has been taught.

SQL is a fourth-generation language and implementations approach it. With fourth-generation languages, the actual query you write is not the thing that runs, and there is an entire process to interpret what you wrote and execute operations that meet the intent of your query in the most efficient manner.

Now, this is where someone chimes in and gives all of the circumstances in which T-SQL (or pick your variant) fails to live up to its fourth-generation heritage, such as particularly complex queries, nested views with multiple joins, you using mechanisms that force a specific plan, etc. This is because real life is messy, as Louis shows in some of the examples.

So what’s the point of the first paragraph, then? Because I never miss an opportunity to talk about language generations.

Leave a Comment

Matching Queries to Indexed Views

Erik Darling has a new video:

Erik Darling here with Darling Data, and in today’s video we’re going to continue on the Learn T-SQL voyage that we have started, and I’m going to talk today a little bit about indexed view matching, because SQL Server is, let’s just call it a mature, or an experienced database engine, and is quite capable, at least in Enterprise Edition, Standard Edition, you do not pay the Microsoft Friendship Tax, so you will be taxed performance-wise, but is quite capable of matching base queries to an indexed view where the syntax matches in some way between them. So, usually exactly between them, not in some way, usually pretty close to just about what you would ask for.

Click through for several tips and, as you experience the frustration of consistently trying to make best use of the view’s index, be glad you’re not trying to get filtered views to work.

Comments closed

Query Execution vs Query Processing

Louis Davidson disambiguates a pair of terms:

There have been a lot of posts on LinkedIn of late about the “logical execution order” of a query that all really miss some really big points. I was corrected myself in some terminology because I mistook the term “processing” to mean the same as “order” in these discussions when I was explaining why logical execution order is not what people expected.

Click through for a good explanation, as well as a plan to have your company pay for your Disney World trip.

I also appreciate how Louis still calls it SQL Sentry Plan Explorer because that’s what it always will be in my heart.

Comments closed