I was messing with SQL query this week and it had been running in less than 10 seconds, then suddenly it was taking over 20 minutes. The entire query was over 150 lines, but we’ll just focus on the area that was problematic:

–31:34 execution time
And here’s the same query, with just a few changes:

–00:02 execution time.
So, usually when I write a post it’s with a problem, an explanation and a solution. I’ve looked at the execution plan and it cannot explain this. So how do I troubleshoot something like this? By running different sections of the overall query, I’m able to determine where the performance is affected and then hone in on that. In this case I was filtering the join with more fields than needed. I was filtering on 4 fields: jobid + wrkctrid + shiftdate + description, and that’s a 30 minute query. after making some changes to the previous subqueries I was able to join on just jobId which is 2 seconds. Further testing revealed that jobid + wrkctrid is fine, and jobid + shiftdate is fine, but jobid + wrkctrid + shiftdate is still not done after 35 minutes.
Probably could be improved with an index if I hadn’t already found a solution for it.




























