Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nestjs GraphQL N+1 문제 #1896

Open
3 tasks done
cho-to opened this issue Jul 29, 2024 · 1 comment
Open
3 tasks done

Nestjs GraphQL N+1 문제 #1896

cho-to opened this issue Jul 29, 2024 · 1 comment
Assignees

Comments

@cho-to
Copy link
Contributor

cho-to commented Jul 29, 2024

Describe the problem and solution

Nestjs Admin GraphQL에서 ResolveField를 활용했을 때, N+1 문제가 발생합니다.

만약 총 5개의 문제를 getProblems에서 가져온다고 할 때, 이때 각각의 tag들을 불러온다면 전체 problem을 조회하는 쿼리 한 번, 각각의 problem에서 tag를 조회하는 쿼리 5번을 요청하게 됩니다.

→ 실제 서버에서도 해봤더니 똑같이 N+1회로 보내집니다.

SELECT * FROM problem;
SELECT * FROM tag WHERE problemId in(1)
SELECT * FROM tag WHERE problemId in(2)
SELECT * FROM tag WHERE problemId in(3)
SELECT * FROM tag WHERE problemId in(4)
SELECT * FROM tag WHERE problemId in(5)

→ 사실 admin 서버가 많이 사용되지는 않을거지만, 미리 최적화하는 것도 괜찮지 않을까요..!

→ 최적화하는건 dataloader 라이브러리를 사용해서 불러온 problemId를 배치에 저장하고, 한꺼번에 tag를 쿼리하는 식으로 한다구 합니다.

Notion task

TAS-

Validations

@jimin9038
Copy link
Member

Failed to connect or create Notion task
(Reason: no assignee)
Please reopen this issue after assigning an assignee

@cho-to cho-to self-assigned this Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Pending
Development

No branches or pull requests

2 participants