mailwal

📬 Post from Darling Data for 10/29/2026 📬

Darling Data
Darling Data

SQL Server Consulting, Education, and Training

10/29/2026

The Joys of Early Exits

The Joys of Early Exits


Watch the Video

Chapters

Full Transcript

Erik Darling here with Darling Data. Sort of a talky video this time. There are great demos to show you this, but it’s more something that I find very useful in my query tuning work that I hope you might find useful in your query tuning work too, and that is the joys of early exits. And by that I mean I see a lot of code written that like long store procedures, long triggers, long whatevers, that could be a lot more efficient if people, developers, robots, anyone out there in the world stopped to look and see if there was actually any work to do before running a whole bunch of queries. So I’m going to talk through some examples of that. Down in the video description you will find all sorts of helpful links, very helpful for both of us, I think. Indeed, we could both benefit in tremendous ways if you hire me for consulting, buy my training, become a supporting member of the channel. You know, a little bit less with the office hours questions, but that’s okay. I still like answering those. I also, not only do I find joy in early exits, but I find joy in giving back. I’m not sure exactly how those two things dovetail, but you know, there we are.

If you enjoy this content, please do like, subscribe, tell a friend. I don’t know. Don’t send me any. Not to my address. That’s weird. If you need free, and speaking of giving back, if you need free SQL Server and or Postgres monitoring, or even Postgres and or SQL Server monitoring, code.erikdarling.com has all you need. In life. Totally free, totally open source. MCP enabled for your robot friends to dig into data. I mean, it just collects all the performance stuff you could possibly ever want. You get a nice WPF viewer if you’re a Windows style tradition trad DBA. You get a nice web viewer if you prefer that. There’s just all sorts of great stuff in there. Pretty charts and graphs, AGs, performance, locking, blocking, deadlocking, CPU, memory, memory, disk, you name it. It’s in there. It’s monitored. There is no greater joy than looking at these dashboards. Also, alerting through various channels. Teams, Slack, PagerDuty.

There’s some other stuff too, I think. I can’t keep track. I don’t need alerts myself, because what am I going to wake myself up for? All right. Windows update messed everything up again. No kidding. Every day. I will be speaking at Pass Summit West in Seattle, Washington from November 9th through 11th. I have two sessions there. One of them is about isolation levels. It’s called Read Committed is Cruel by Default.

And the other one is my full day, Magnificent Advanced T-SQL Pre-Con. And you should come to both, because I think you’re pretty, and you’re smart, and you smell great, and I want to be in a room with you. I don’t know what other charms I can break out for this, but probably not anything without not being disinvited from the conference. So, whenever I see a lot of stored procedures, a lot of code in general, a lot of T-SQL in general, where, you know, maybe, you know, a temp table gets populated, or you’re looking for data that meets a particular condition, but, like, the code doesn’t stop if nothing gets found.

So, like, let’s say you do your initial insert into a temp table, and no rows end up in that temp table. You can check that very cheaply and very easily with row count, or add-at row count, or row count big, which is my preference, because I deal with some big row counts in my life. And if you don’t find anything, just stop there. That’s also true in triggers. The very first thing you should do in a trigger before you, you know, set no count on, or whatever other things you want to do, is just say, if row count big equals zero, return, right? Just stop right there, right? Nothing, like, an insert, update, delete, or merge ran, but nothing changed. Just cut it out, right?

Don’t go to the next queries. Don’t, like, say, do some stuff if stuff exists in the inserted or deleted table. Just cut it right there, right? And by extension, I see a lot of code where, ah, where the robots are testing stuff. I guess I got interrupted. God bless, right? No hands, right? They’re doing a click-through before I do a release. By the same extension, like, there are any number of things that that might work for.

Like, let’s say we’re in the Stack Overflow database, and we’re like, ah, run this series of queries and do all this stuff, but only if, like, a post type ID equals, I don’t know, 23, and it’s, like, the current date. But if that doesn’t exist, we don’t have to go do all that work. And this is, this can get you in two different ways. One, queries like this, like, even, you know, just, like, polling queries, stuff like that.

Maybe you have jobs that run and keep doing stuff and looking for work, and they run through all the code no matter what. No one is just saying, hey, if we don’t get anything here, bail here immediately. There’s no need to do all this stuff. Some people run into problems with that because whatever is calling the code might expect some results, and a lot of APIs are like that.

So if you, if your API is still expecting stuff, well, you can just select a list of dummy columns from nothing and return an empty result, right? So instead of just returning, you can just say, select column list, right? You know, null equals all this stuff across the board. Just be careful with data types there because SQL Server thinks nulls are integers.

Might, you might find yourself breaking some API contract if you return an integer maybe where you shouldn’t. Integers are pretty flexible, though, so maybe that’s not the worst thing in the world. But, again, this can bite you in two ways. One is code that executes very frequently.

You know, like, say you have something that runs, you know, hundreds of thousands or millions of times a day. The early bailout is so valuable there because you no longer have to execute lots of tiny little queries that end up not returning anything. There are some times where it’s, you know, pretty cheap and easy.

If it’s just like a couple queries in a store procedure, it might not be the biggest deal in the world. But even if it’s just like 200 milliseconds worth of, you know, CPU or duration or something, it could be zero, right? You can, you can save many, many CPU hours a day by bailing out early.

That number can, can get bigger and it can also, you know, get small. Numbers can be big or small and they can be good or bad in either direction. But, you know, you might have other queries that do a whole lot of extra work.

Or maybe even queries that attempt to take a whole bunch of weird locks when they don’t have to. And, and you can stop all that very early on by just looking for early exits in your code. But I think that’s, that’s probably about all I got for this one.

I promise this would be a short video. So we’re, we’re, we are going to keep this one under 10 minutes because I know that is, you know, smart and pretty and funny and good smelling as you are. You do have a limited attention span.

So I’m going to let you go. But I hope you will keep this in mind as you are looking for queries to tune, right? Because, you know, as, as, as a wise person once said, the fastest query you can run is one, the fastest query is one that doesn’t run.

There we go. Those are the words. And if, if you can run far fewer queries, you can save a whole lot of time in your SQL servers.

All right. Well, I’m going to let the robots get back to testing the walkthrough here. Uh, and I will see you next Tuesday with an office hours video.

All right. Thank you for watching.

Going Further


If this is the kind of SQL Server stuff you love learning about: these days I have AI do the annoying stuff for me. My new video course teaches you to work the same way, and I teach whole teams in a one-day workshop. Someone still has to know enough SQL Server to keep the robots in check, and that’s what my training is for.

The post The Joys of Early Exits appeared first on Darling Data.

Training

Get AI-Ready With Erik

Vectors, embeddings, and AI in SQL Server 2025

Learn T-SQL With Erik

Write better T-SQL from the ground up

Performance Engineering

Query tuning, indexing, and parallelism

The Everything Bundle

All courses, best price

Copyright © 2026 Darling Data, LLC. All rights reserved.
You are receiving this email because you opted in via our website.