Query tweak - fixing issue with possible temp tables causing huge performance impact