
한 게시글에 문의 데이터 500개

한 문의에 대해 답변 1개
1.
select
q1_0.idx,
q1_0.answer_status,
q1_0.content,
q1_0.created_at,
q1_0.modified_at,
q1_0.product_board_idx,
q1_0.title,
q1_0.user_idx
from
question q1_0
where
q1_0.product_board_idx=?
order by
q1_0.created_at desc limit ?,
?
2.
select
count(q1_0.idx)
from
question q1_0
where
q1_0.product_board_idx=?
3.
select
a1_0.question_idx,
a1_0.idx,
a1_0.company_idx,
a1_0.content,
a1_0.created_at
from
answer a1_0
where
a1_0.question_idx=?
4.
select
c1_0.idx,
c1_0.company_name,
c1_0.email
from
company c1_0
where
c1_0.idx=?
5.
select
u1_0.idx,
u1_0.email,
u1_0.name
from
user u1_0
where
u1_0.idx=?
6.
select
p1_0.idx,
p1_0.category_idx,
p1_0.company_idx,
p1_0.created_at,
p1_0.discount_rate,
p1_0.ended_at,
p1_0.minimum_price,
p1_0.modified_at,
p1_0.product_detail_url,
p1_0.product_thumbnail_url,
p1_0.started_at,
p1_0.status,
p1_0.title
from
product_board p1_0
where
p1_0.idx=?
7.
select
a1_0.question_idx,
a1_0.idx,
a1_0.company_idx,
a1_0.content,
a1_0.created_at
from
answer a1_0
where
a1_0.question_idx=?
8.
select
a1_0.question_idx,
a1_0.idx,
a1_0.company_idx,
a1_0.content,
a1_0.created_at
from
answer a1_0
where
a1_0.question_idx=?
9.
select
a1_0.question_idx,
a1_0.idx,
a1_0.company_idx,
a1_0.content,
a1_0.created_at
from
answer a1_0
where
a1_0.question_idx=?
10.
select
a1_0.question_idx,
a1_0.idx,
a1_0.company_idx,
a1_0.content,
a1_0.created_at
from
answer a1_0
where
a1_0.question_idx=?
product_board_idx의 문의 목록을 최신 순으로 조회Answer 데이터를 개별적으로 조회ProductBoard 정보 조회각 문의에 대해 개별적으로 답변을 조회하고, 판매자 정보, 작성자 정보, 상품 정보를 따로 조회하면서 중복된 쿼리 호출이 발생했습니다. 그 결과, 총 10번의 SELECT문이 실행되어 데이터베이스에 불필요한 부하를 줌.

→ 평균 응답 시간 20.65(ms)
@BatchSize를 적용하여, Question과 Answer 간의 지연 로딩 문제를 개선했습니다. 기존에는 각 문의마다 답변을 개별적으로 조회해 N+1 문제가 발생했지만, 이제는 한 번에 최대 5개의 답변을 가져오도록 변경해 쿼리 호출 수를 줄였습니다.QuestionRepository에서 JOIN FETCH를 사용해 Question과 ProductBoard를 한 번에 조회하도록 쿼리를 수정했습니다. 이전에는 지연 로딩으로 인해 상품 정보를 추가로 불러오는 쿼리가 발생했지만, 이제는 두 데이터를 함께 가져와 불필요한 쿼리 호출을 줄였습니다.1.
select
q1_0.idx,
q1_0.answer_status,
q1_0.content,
q1_0.created_at,
q1_0.modified_at,
p1_0.idx,
p1_0.category_idx,
p1_0.company_idx,
p1_0.created_at,
p1_0.discount_rate,
p1_0.ended_at,
p1_0.minimum_price,
p1_0.modified_at,
p1_0.product_detail_url,
p1_0.product_thumbnail_url,
p1_0.started_at,
p1_0.status,
p1_0.title,
q1_0.title,
q1_0.user_idx
from
question q1_0
join
product_board p1_0
on p1_0.idx=q1_0.product_board_idx
where
q1_0.product_board_idx=?
order by
q1_0.created_at desc limit ?,
?
2.
select
count(q1_0.idx)
from
question q1_0
join
product_board p1_0
on p1_0.idx=q1_0.product_board_idx
where
q1_0.product_board_idx=?
3.
select
a1_0.question_idx,
a1_0.idx,
a1_0.company_idx,
a1_0.content,
a1_0.created_at
from
answer a1_0
where
a1_0.question_idx in (?,?,?,?,?)
4.
select
c1_0.idx,
c1_0.company_name,
c1_0.email
from
company c1_0
where
c1_0.idx=?
5.
select
u1_0.idx,
u1_0.email,
u1_0.name
from
user u1_0
where
u1_0.idx=?
JOIN FETCH로 한 번에 조회@BatchSize 적용으로 각 문의에 대한 답변을 묶어서 조회 (IN 사용)
→ 평균 응답 시간 13.95(ms)
| 개선 전 | 개선 후 | |
|---|---|---|
| 1 | 29 ms | 25 ms |
| 2 | 29 ms | 21 ms |
| 3 | 16 ms | 18 ms |
| 4 | 21 ms | 10 ms |
| 5 | 31 ms | 16 ms |
| 6 | 20 ms | 12 ms |
| 7 | 23 ms | 20 ms |
| 8 | 15 ms | 12 ms |
| 9 | 20 ms | 14 ms |
| 10 | 28 ms | 7 ms |
| 11 | 23 ms | 15 ms |
| 12 | 24 ms | 14 ms |
| 13 | 14 ms | 12 ms |
| 14 | 23 ms | 9 ms |
| 15 | 8 ms | 19 ms |
| 16 | 19 ms | 16 ms |
| 17 | 17 ms | 6 ms |
| 18 | 17 ms | 20 ms |
| 19 | 20 ms | 6 ms |
| 20 | 16 ms | 7 ms |
| 평균 | 20.65 ms | 13.95 ms |