T-SQL Query Exercise: Generate Big Spills

Query Exercises
18 Comments

When I was testing SQL Server 2025’s new ability to limit TempDB usage with Resource Governor, I wrote a few fun diabolical demos. One of them was to generate giant spills to TempDB, quickly.

When you’re looking at an actual (not estimated) query plan, and you see a yellow exclamation point on a sort operator, that means:
Fundamentals of TempDB

  • SQL Server made a guess about how much memory it would need to accomplish that sort
  • The amount of data coming into the sort wildly exceeded SQL Server’s plans
  • SQL Server resorted to writing that data to TempDB: aka, the data spilled over from memory to disk

This can happen in a few interesting ways:

  • SQL Server under-estimates how much data will be coming into the sort, or
  • SQL Server accurately estimates it, but simply doesn’t have enough RAM available to handle it
  • The query goes parallel, the memory is evenly divided across threads, but the data is not evenly distributed, so some threads run out of memory while other threads’ memory sits unused

Sorts aren’t the only operators that can spill, either!

So for this week’s Query Exercise, we’re going to play a little code golf: write the shortest query you can that will produce the biggest spills. Code golf is a fun way to think out of the box, freeing yourself from traditional limitations around the tables & queries you usually work with.

Those of you who have my Fundamentals of TempDB course can check out this brand-new class module on Resource Governor, and you’ll get an idea of the approach I took. (If you don’t have that class, check out my Black Friday sale.) I couldn’t let this problem out of my head so I ended up toying around with it, iterating over that approach for a while to make it much shorter than the class demo.

Put your queries in a Github Gist and include those link in your comments. Check out the solutions from other folks, compare and contrast your work, and next week I’ll circle back with my answers & thoughts. To give you a rough idea of what to aim for, my solution uses less than 100 characters, and will run any server out of TempDB space with a ginormous spill. Have fun!

Previous Post
SQL Server 2025 Is Out, and Standard Goes Up to 256GB RAM, 32 Cores!
Next Post
Functions in the WHERE Clause Are Bad… Right?

18 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.