Next.js + Prisma 조합으로 API를 만들고 있는데, 유저 목록과 각 유저의 게시글 수를 함께 가져오려고 합니다.
현재 코드가 이렇습니다:
const users = await prisma.user.findMany()
for (const user of users) {
const count = await prisma.post.count({ where: { authorId: user.id } })
}이렇게 하면 유저 수만큼 쿼리가 나가서 느린데, Prisma에서 N+1 문제를 어떻게 해결하나요? include나 _count 같은 걸 쓰면 되나요?
댓글 0