sovle n+1 queries problem