In my experience, the more complex your queries, the less capable the SQL optimizer is to create clever plans. Here you have 16 associations, some or most of them are external connections, you have at least one subquery ... throw enough indexes, powers, views, external applications, and who knows that no one else, not even Microsoft Engineers *, can determine the procedures that will evenly and regularly generate the most optimal plans.
What you described, I have experienced many times - change one simple thing in a messy request and everything is an order of magnitude faster (or, grinding your teeth, slower). I have no way to determine when the complex is too complex, it is more a feeling than anything else. My general rule is that if it looks too long or too complicated, simplify where you can, for example, your pre-selected one-time nested value or snatch out a part of the query, which will always be executed quickly with a small set of results, and starts it first , and saves the results in the temp table.
(* Please note that this is a soft sarcsam)
source share