• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

(추가) 다대다 페이징 질문드립니다!

22.12.24 13:22 작성 조회수 312

0

(이전 질문)

https://www.inflearn.com/questions/718059/%EC%97%AC%EB%9F%AC-%ED%85%8C%EC%9D%B4%EB%B8%94-fetch-join-%EC%A7%88%EB%AC%B8-%EB%93%9C%EB%A6%BD%EB%8B%88%EB%8B%A4

 


안녕하세요. 이전 질문에 다시 질문 좀 드리고 싶어서 추가 질문 드립니다..ㅠㅠ

 

1)

이전 질문에서 다대다 관계(Event <-> EventArtist<-> Artist)에서 이벤트 리스트를 가져올때 이벤트에 포함된 아티스트의 이름 정도만 가져와야 되면 fetch join 하는 것보다 join + dto로 해결하는 방법을 추천해주셨습니다.

그런데 하나의 이벤트를 조회하는 경우 아티스트의 이름 리스트는 join + dto로 해결할 수 있는데 이벤트를 리스트로 가져오는 경우에도 말씀해주신 join + dto로 가져올 수 있을까요??

 

2)

그리고 이벤트를 페이징 처리를 하려고 하니 컬렉션을 페이징하면 인메모리를 사용해서 위험하고 중간 엔티티로 페이징을 하면 이벤트가 기준이 아니어서 제대로 페이징이 안 되었습니다.

그래서 이벤트를 Batch Size로 먼저 조회를 하고 List<Event>를 중간 엔티티 EventArtist에 넘겨서 In 쿼리로 조회를 했습니다. (이벤트를 batch size로 조회만 하면 아티스트에 접근할때 N + 1 쿼리 발생)

 

혹시 이 방법에 대한 피드백도 좀 주시면 정말 감사하겠습니다..!

서비스 코드

// event 리스트를 먼저 조회 (batch size)
List<Event> events = eventRepository.findEvents(lastIndex, PageRequest.of(0, 3));
// fetch join + in 쿼리
List<EventArtist> eventArtists = eventArtistRepository.findEventArtists(events);

 

  1. 이벤트 batch size로 페이징 조회

@Query("select e from Event e " +
        "where e.id >= :lastIndex")
List<Event> findEvents(@Param("lastIndex") Long lastIndex, Pageable pageable);

결과 (간단하게 수정)

select

event.*

from

events event

where

event_id>=? limit ?

 


 

  1. 조회한 이벤트로 join fetch + in 쿼리를 해서 아티스트 조회

@Query("select ea from EventArtist ea " +
        "join fetch ea.artist " +
        "where ea.event in :events")
List<EventArtist> findEventArtists(@Param("events") List<Event> events);

 

결과 (간단하게 수정)

select

event_artist.*,

artist.*

from

event_artists event_artist

inner join

artists artist

on event_artist.artist_id = artist_id

where

event_artist.event_id in (

? , ? , ?

)

 

답변 1

답변을 작성해보세요.

0

안녕하세요. code-tree님

다음 강의에 궁금해하시는 내용들을 설명하고 있습니다. 다음 강의를 복습해주세요.

실전! 스프링 부트와 JPA 활용2 - API 개발과 성능 최적화

감사합니다.