Here what can be done during a session of SQL performance tuning.
This query was running for a long time, causing a blocking chain and High CPU usage.
It was also preventing other sessions and the index maintenance job from completing.
All the tests were executed on the DEV server against a database restored from the PROD server.
The query was executed a few times and tested possible solutions to improve the performance and fix the query issue.
After many tests, it was clear that the problem was a NOT IN operation calling a view without a where clause, which causes a massive number of reads.
The NOT IN operation for a NOT EXISTS operation was changed as per the query below (See the highlights).
Comparing the results
As expected, the same output (result set) was presented by the original and modified query.
Small changes can lead to big improvements!
But take care, the logical operators NOT IN and NOT EXISTS behave different under certain circumstances, or performance-wise, meaning if using one operator has a performance benefit over the other.