Optimized code further by taking union all first