r/SQLServer • • May 31 '22

Holy sort batman

Post image
58 Upvotes

24 comments sorted by

4

u/messed_up_alligator May 31 '22

Is that estimate accurate? Sweet jesus lol. After a distinct at that

If it's not accurate, any clues to what the query looked like to get such a large overestimate?

2

u/ArtooSA Jun 01 '22

It was accurate.

It was basically: Select from table1 Left join table2 on NotThePK = NotTheFK Left join table3 on NotThePK = NotTheFK Left join table4 on NotThePK = NotTheFK Etc etc

6

u/Apogea May 31 '22

A few cross joins instead of inner/outer of course!

2

u/mikeblas Jun 01 '22

It's easy to fake a query plan. And not much harder to make a query with massive cardinality that you never execute.

0

u/[deleted] May 31 '22

[deleted]

11

u/da_chicken Jun 01 '22

Yeah my guess is accidental cross join, probably from an alias error:

FROM TableA a
    JOIN TableB b
        ON a.Id = a.Id

I've definitely made this kind of mistake before.

3

u/ArtooSA Jun 01 '22

Definitely an accidental cross join, but not an alias error. It was a brain error. The guy couldn't explain what a join was, and eventually admitted he copied some code and made some random changes hoping it would work.

1

u/da_chicken Jun 01 '22

LOL Fair enough. Silly me assuming people know what they're doing and are making honest mistakes!

4

u/rbobby Jun 01 '22

And people called me crazy for using TableAId and TableBId. Who's crazy now! Hahahahahhahahahahahahah

8

u/LurkerNumber44 May 31 '22

lolz. i can't wait to see what Brent says.

1

u/kagato87 Jun 01 '22

Brent would probably figure out a way to make that synthetic query run...

4

u/ArtooSA Jun 01 '22

It's not a case of fixing the query, rather a complete rewrite. The developer basically threw a whole bunch of random left joins together, with a distinct to remove the excess. I actually needed to use a defibrillator on tempdb after that 😅

1

u/ArtooSA Jun 01 '22

It's not a case of fixing the query, rather a complete rewrite. The developer basically threw a whole bunch of random left joins together, with a distinct to remove the excess. I actually needed to use a defibrillator on tempdb after that 😅

3

u/PedroAlvarez May 31 '22

Seven thousand dollars per cooooore

2

u/FriendlyDisorder Jun 01 '22

The SQL Server query optimizer does a great job 99% of the time. The remaining 1% keeps me employed. 😏

3

u/ArtooSA Jun 01 '22

Query optimiser cannot optimise this particular junior developer. He joined 10 tables on random columns (recursive Cartesian products) and didn't get the expected results. So he made them all left joins, effectively removing any filters, and did a distinct.

He actually complained that I didn't configure tempdb correctly when his query failed because tempdb ran out of space.

1

u/FriendlyDisorder Jun 01 '22

Ha! Great story. I have had the same experience before. Nothing can defeat the power of the Junior Developers-- not even the mighty Query Optimizer!

3

u/phunkygeeza Jun 01 '22

Good job that is a parallel query, should only take a few years instead of a millennium or so.

2

u/IrishAce007 May 31 '22

484 trillion estimated rows? Damn!

1

u/dblue_one Jun 01 '22

Dev that made that query deserves a email spam, half that number should be enough.

0

u/[deleted] Jun 01 '22

[deleted]

3

u/[deleted] Jun 01 '22

[removed] — view removed comment

2

u/ArtooSA Jun 01 '22

It was excessive memory grant lol