How to See Rowcounts and Execution Time for In-Flight Queries in SQL Server
I frequently need to see rowcounts and execution time for queries while they’re running. Maybe I’m troubleshooting a slow query …
Read Moreon • 6 min read
Over the years I’ve gotten lots of emails and questions from students that start like this:
Help! My partitioned table has the wrong data in a partition! It’s lopsided. I started trying to fix it, but…
The next sentence is one of a few things. Sometimes it’s all of them.
The story usually then goes on:
And then the question: What do I do now?
First, let’s explain a bit about what went wrong. When you set up partitioned tables in SQL Server, you define “boundary points” as part of your Partition Function. This explains logically how the data is laid out.
For a simple example, let’s say we have a table named dbo.SalesHistory. It’s partitioned on the SalesDate column, and we’ve defined these boundary points using “Range Right” so that the boundary point itself stays with the data following it:
Everything was fine, but we forgot to put a boundary point in for Jan 1, 2016… and now it’s well into January. All our data for January 2016 is in the partition with the December data.
The more time passes, the more data is going to go into that partition, and it’ll get bigger and more lopsided. If we’re switching out old partitions by month, eventually that’s not going to work. And partition elimination won’t work for anything after Dec 1, 2015, either!
The first fix that most people try is simply adding in the boundary point for Jan 1, 2016 by altering the partition function and using SPLIT RANGE. For small partitions, that can work well.
But usually partitioned tables are quite large. And usually nobody notices this for a while. The “lumpy” partition typically has millions and millions of rows and may be hundreds of gigabytes of data. Just running SPLIT can be a disaster.
This post by Kalyan Yella and Denzil Ribeiro explains why: The SPLIT operation is actually doing two fully logged operations behind the scenes in one giant transaction: a DELETE and an INSERT. This is why it’s prone to be time consuming and use vast amounts of transaction log space.
This workaround is not perfect: your data will be offline while you do it.
This isn’t something I came up with on my own, either– it’s a modified version of the recommendation in the post I linked above, and it’s also been recommended to limit log growth when splitting partitions by Microsoft Program Manager Sunil Agarwal.
But if you can take a downtime, I find this to be simple to code, test, and optimize.
You can potentially minimize the downtime by optimizing the order like this, as long as data isn’t changing:
If data is changing, you’ll have to get fancier, using triggers or some other method to make sure you don’t miss rows.
Pick the method that’s right for you depending on your comfort scripting and tolerance for downtime.
Minimal logging is just what it sounds like: a way you can insert a lot of data without writing all of it to the transaction log. It can really speed up operations like this, particularly if you’ve got a lot of data and aren’t on the world’s fastest storage. It only works in the SIMPLE and BULK LOGGED recovery models, but doing the workaround requires an outage, anyway.
For information on how to get minimal logging, read the Microsoft Data Loading Performance Guide by Thomas Kejser, Peter Carlin and Stuart Ozer.
Long term, you just don’t want to get into this situation. Anywhere you use partitioning (and are NOT using the rotating log pattern), you should have a job set up that checks and makes sure you have at least two empty partitions ahead of your highest boundary point. The job should fail and send an alert to someone who knows how to fix it if you get into the danger zone.
Why two partitions? That person can’t always drop whatever they’re doing to fix it right away. If you work in a large company, three partitions might even be better for you.
Copyright (c) 2025, Catalyze SQL, LLC; all rights reserved. Opinions expressed on this site are solely those of Kendra Little of Catalyze SQL, LLC. Content policy: Short excerpts of blog posts (3 sentences) may be republished, but longer excerpts and artwork cannot be shared without explicit permission.