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

TopicResponse 쿼리 최적화 #48

Open
60jong opened this issue Dec 2, 2023 · 1 comment
Open

TopicResponse 쿼리 최적화 #48

60jong opened this issue Dec 2, 2023 · 1 comment
Labels

Comments

@60jong
Copy link
Collaborator

60jong commented Dec 2, 2023

페이징으로 인해 일대다 관계는 fetch X
-> 한 요청 당 4개의 query가 요청됨. main / topic -> topickeyword / topickeyword -> keyword / topic -> choice)

# 1
SELECT t1_0.*,
       a1_0.*
FROM   topic t1_0
       JOIN member a1_0
         ON a1_0.id = t1_0.author_id
       JOIN topic_keyword t2_0
         ON t1_0.id = t2_0.topic_id
WHERE  t1_0.status =?
       AND NOT EXISTS(SELECT 1
                      FROM   hidden_topic h1_0
                      WHERE  h1_0.topic_id = t1_0.id
                             AND h1_0.member_id =? )
ORDER  BY t1_0.vote_count DESC
LIMIT  ?, ? 

lazy_loading -> batch 요청

# 2
select * from topic_keyword t1_0 where t1_0.topic_id in (?, ?)

# 3
select * from keyword k1_0 where k1_0.id in (?, ?)

# 4
select * from choice c1_0 where c1_0.topic_id in (?, ?)

!!! query를 topic_keyword를 기반으로 시작해도 main + 3 = 4번의 쿼리 나감

@60jong
Copy link
Collaborator Author

60jong commented Dec 11, 2023

Keyword 3 -> 1 변경으로 수정

SELECT t1_0.*,
       a1_0.*
FROM   topic t1_0
       JOIN member a1_0
         ON a1_0.id = t1_0.author_id
       JOIN keyword k
         ON t1_0.id = k.topic_id
WHERE  t1_0.status =?
       AND k.ke
       AND NOT EXISTS(SELECT 1
                      FROM   hidden_topic h1_0
                      WHERE  h1_0.topic_id = t1_0.id
                             AND h1_0.member_id =? )
ORDER  BY t1_0.vote_count DESC
LIMIT  ?, ? 

where 절 내부의 subquery를 join으로 해결하고 픔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants