Goal Setting
Every year, I see posts like this one https://www.sqlskills.com/blogs/paul/2025-the-year-in-books/ from Paul Randal, and over time I have started to also collect some of my own metrics and to set some personal goals. I’ve been following Paul’s blog since 2014, but I didn’t set any personal goals until I was working from home during COVID-19. I was having trouble disconnecting from work at the end of the day. I no longer had a commute and would find myself working hours longer than I intended. Setting goals is what ended up helping me get out of a rut, specifically reading goals. Here’s a bit of a timeline of where I’ve been since then: ...
T-SQL Tip 20: Nice to Have Columns
You ever ask yourself: “Hey self, who updated this row last?” or “When was this row updated last?” Well bad news/good news. Bad news. SQL Server doesn’t store this by default. Good news. You can setup logic to store it anyways. The way I store this is when I build a table I add a LastUpdated datetime column and an UpdatedBy varchar(70) column. I then set these so they have a default constraint of GETDATE() for LastUpdated and SYSTEM_USER for UpdatedBy. If you add these columns you can start to answer these questions, but you’ll also have to update these columns when you run an UPDATE statement. ...
T-SQL Tip 19: Archival Acceleration
Archiving data that is ready to be archived will help you get results faster. It’s one of those things that improves every aspect of the database engine. Less data to look at on storage. Less data to bring into memory. Less data to run a calculation on for CPU. Less data to send over the network to the client. Less data to keep stats on for plans. Every resource SQL Server relies on for generating a result set is improved when there’s less data to evaluate. The hard part is that “ready to be archived” bit, but it can make such a large impact that you should think about archiving long before you need it. ...
T-SQL Tip 18: Sort Somewhere Else (if you can)
A bit tongue in cheek and light-hearted this time. SQL Server can sort. It doesn’t generally do it well. You pay a whole lot per SQL Server CPU. Why should we spend a lot of money to have it do something it does poorly? When possible you will get performance gains from sorting in app, and make all the other database users happier.
T-SQL Tip 17: Stored Procs, Not Adhocs
Stored procedures are T-SQL that is compiled for later execution. They are able to accept parameters (these parameters can even be table types that you pass a table variable (see T-SQL Tip 16)). One of the benefits of stored procedures is everything that uses the SP does the same work, the same way. More likely to have metrics, and plan reuse. Since they accept parameters on input (and output), these parameters in execution plans get parameterized. This means that SQL Server doesn’t use your literal input value to form the plan but something generic. This makes the plans consistent for repeat runs, for better or worse though. ...
T-SQL Tip 16: Table Variables
Last tip, T-SQL Tip 12, was on temp tables. Another object that will allow you to hold onto and reuse results is a table variable. These are created with an @ symbol and are created in a declare statement, @ThisIsAVariable. They only persist as long as the batch they are declared in. If you store the results of a query in a table variable, you can use that variable for further queries in your batch. We talked about execution plans in T-SQL Tip 8, and these objects are unique in some very specific limitation on those plans. SQL Server does a poor job at guessing how much data is in them, with old enough versions assuming just 1 row. For large enough table variables, this can make queries run real bad. Despite that they are great for passing results into stored procedures, even if you then dump to a temp table inside the SP. ...
T-SQL Tip 15: Temporary Like Presidents
SQL Server lets you build temporary tables using the same code as a normal table. These are created using a # sign in front of the table name, #ThisIsTemporary. These only stick around as long as the session they are created in. If you use two ## signs it is created globally and other sessions can see the table with the name specified. These are a great way to help SQL Server do a single workload one time instead of repeatedly. You can run the initial workload and store the results to a temp table; then, you can keep referencing that temp table without repeating that work. These can also help to make complex things more bite-sized and tunable for you, and SQL Server. ...
T-SQL Tip 14: It Depends
I may have misled you a bit. Every tip so far, and probably in the future too, has exceptions. SELECT * is bad….. unless you don’t know what’s in the table yet… Every table should have a clustered index and primary key….. unless it’s temporary table… Scans are better than seeks….. unless you want a lot of ordered data… As our topics grow more complex, the space for exceptions to the rules will grow larger. Most things you encounter should follow these rules, but hey.. It depends. ...
T-SQL Tip 13: Recap Episode - New Tables
Let’s combine everything we’ve covered lately to make a new table. This new table should have a clustered index, and the most likely clustered index would be an IDENTITY column using int or bigint. Given its inherent uniqueness it will also make a great primary key. For the other columns, we should double think any max datatype columns. Try to instead store these in smaller chunks, or as-is with a smaller datatype. One benefit of this will be many columns become indexable. Opening up an execution plan, we can make sure SELECT and UPDATE statements have their WHERE clause and JOIN predicates using a non-clustered index. ...
T-SQL Tip 12: Default Clustered Index
We talked about why you should use a clustered index in T-SQL Tip 4. Now let’s talk about what to make the clustered index. Here’s an acronym for things that make a good clustered index: One Column: Small data footprint, generally not strings, one column Unique: Something that by design won’t repeat Increasing. You won’t put a row in the middle of what already exists Immutable: Unchanging, and not going to be updated It’s for those characteristics that I generally recommend an int or bigint IDENTITY column. ...