Say you’re at Dell DBA Days, and you want to build a really ugly query, fast. You want to generate a StackOverflow database query that will take a long time to compile, and maybe demand a huge amount of memory to run, but not actually take any time to execute.
You might start by building a numbers table with Method #7 from this StackOverflow answer , and then:
SELECT 1 AS Ordered, 'SELECT * FROM dbo.Posts p1'UNION
SELECT nt.Number AS Ordered, 'INNER JOIN dbo.Posts p' + CAST(nt.Number AS VARCHAR(10)) +
' ON p' + CAST(nt.Number AS VARCHAR(10)) + '.Id = p' + CAST(nt.Number - 1 AS VARCHAR(10)) + '.ParentId'
FROM dbo.NumbersTest nt
WHERE nt.Number > 1
ORDER BY 1
Which gives you a 10,000 join query, but I’m only showing the first 10 lines here:

Big dynamic query
So then you can copy/paste the contents of that second column into a new window. You could also assemble a string, but I like having it available for copy/paste because it, uh, doesn’t exactly work the first time.
For example, when I run it with 10,000 joins:
Msg 8631, Level 17, State 1, Line 1Internal error: Server stack limit has been reached. Please look for potentially deep nesting in your query, and try to simplify it.
When I drop it down to a much more realistic 5,000 joins:
Msg 4074, Level 16, State 1, Line 1Client drivers do not accept result sets that have more than 65,535 columns.
Ah! Okay, that’s fair. (That’s also two error messages I’ve never seen before. Probably a good thing.) Alright, let’s take out the SELECT * and replace it with SELECT p1.* and see what happens:
Msg 8621, Level 17, State 1, Line 1The query processor ran out of stack space during query optimization. Please simplify the query.
Come on, SQL Server. Work with me. Cowboy up. Alright, let’s try 2,000 joins, and….

Fun trivia: SQL Server uses exactly one core to compile a query.
Fifteenminutes later, SQL Server is still trying to compile an execution plan for the two-thousand-joinquery and I know for a fact that the query won’t produce a single row.
So as long as I’m waiting for it to compile, might as well have a little fun with Task Manager. On a 72-core box, this query:
ALTER SERVER CONFIGURATION SET PROCESS AFFINITY CPU = 0 TO 1,4 TO 5,7,10,12,14,16,19,22,24,26,28 TO 29,31,34,64,66,68 TO 69,71,74,76,78,80,83,86,88 TO 89,92 TO 93,95 TO 96,98 TO 99
GO
When combined with a really ugly CPU-burning query, you get:

Graffiti in Task Manager
Over an hour later, it was still trying to build a query plan for the 2,000 join query, so I figured hey, let’s put affinity masking back the right way, then throw the query into SQLQueryStress and run 72 executions of it:

FIRE IN THE HOLE
Which gives me a slightly different Task Manager:

High score!
It’s surprisingly hard to achieve 100% CPU usage across all 72 cores, but be patient, and you can get it. SQL Server is in bad shape, too if you runsp_BlitzFirst to check the running queries:

The elusive RESOURCE_SEMAPHORE_QUERY_COMPILE
Most queries are waiting on RESOURCE_SEMAPHORE_QUERY_COMPILE which means they can’t get enough memory in order to start compiling.
I canceled that fun, but not all that useful and over 90 minutes later, I was still waiting for the 2,000 join query to compile.
Wanna watch these kinds of shenanigans live? Sign up for today’s Dell DBA Days webcasts.
Update: after 12 hours and 43 minutes, the compilation gave up:
SQL Server puts in half-days
Brent Ozar

I make Microsoft SQL Server faster and more reliable. I love teaching, travel, and laughing.




