Minor sql optimisation to remove n+1s