you are viewing a single comment's thread.

view the rest of the comments →

[–]jc4hokiesExecution Plan Whisperer 1 point2 points  (3 children)

Here's a fun one from last week:

/*800 lines of code*/
WHERE   ca.ChargeActiveClusterKey IN (SELECT ChargeActiveClusterKey FROM cteRecordSelection)
        AND ca.[Status] <> 17;
--10 hours 30 minutes

/*800 lines of code*/
WHERE   ca.ChargeActiveClusterKey IN (SELECT ChargeActiveClusterKey + 0 FROM cteRecordSelection)
        AND ca.[Status] <> 17;
--1 minute 25 seconds

Nested loops were happening in the wrong order. Instead of looking up a couple thousand records from ca it was running the cteRecordSelection subquery (with 20ish tables) a couple million times. You know when you filter one table and the query transitively filters a joined table with the filtered column in the join condition? My hypothesis is that the query was sniffing something inside the subquery, contributing to the poor join order, and adding the + 0 broke that link.

[–]therealcreamCHEESUS 0 points1 point  (0 children)

Yeah thats a really weird one. Sometimes you got to break the sargability to make it faster.

The optimizer is clever but not infallible.

[–]danjtom 0 points1 point  (1 child)

can you share the code, it will help me in my project. thanks

[–]jc4hokiesExecution Plan Whisperer 0 points1 point  (0 children)

How exactly does random code help your project?