Skip to content

Commit

Permalink
ShortForm Repository의 Fetch Join과 Pagination query 분리
Browse files Browse the repository at this point in the history
ShortForm Repository의 Fetch Join과 Pagination query 분리
  • Loading branch information
xGreenNarae authored Oct 30, 2023
2 parents d82c3e2 + 6f8dd20 commit 21809a8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand All @@ -25,33 +24,37 @@ public class ShortFormService {
private final PetVideoRepository petVideoRepository;

public HomeShortFormPage getHomeShortFormPage(final Pageable pageable) {
// LikeCount DESC 순서로 조회하고, 반환된 페이지(Slice)를 랜덤으로 섞는다.
Slice<PetVideo> petVideoSlice = petVideoRepository.findSliceBy(pageable);

// Fetch Join + Pageable 동시에 수행하는 경우 발생하는 문제(HHH000104) 해결을 위해 쿼리를 두 개로 분할하였습니다.
Slice<Integer> petVideoIdSlice = petVideoRepository.findSliceOfIds(pageable);
List<PetVideo> petVideos = petVideoRepository.findAllByPetVideoIdIn(petVideoIdSlice.getContent());

// LikeCount DESC 순서로 조회하고, 반환된 페이지(Slice)를 랜덤으로 섞는다.
return HomeShortFormPage.of(
shuffleVideos(petVideoSlice),
petVideoSlice.hasNext()
shuffleVideos(petVideos),
petVideoIdSlice.hasNext()
);
}

public CategoryShortFormPage getCategoryShortFormPage(final ShortFormSearchCondition searchCondition,
final Pageable pageable) {
Slice<PetVideo> petVideoSlice = petVideoRepository.findSliceBy(

Slice<Integer> petVideoIdSlice = petVideoRepository.findSliceOfIds(
searchCondition.type(),
searchCondition.area(),
pageable
);
List<PetVideo> petVideos = petVideoRepository.findAllByPetVideoIdIn(petVideoIdSlice.getContent());

return CategoryShortFormPage.of(
buildCategoryPageTitle(searchCondition),
shuffleVideos(petVideoSlice),
petVideoSlice.hasNext()
shuffleVideos(petVideos),
petVideoIdSlice.hasNext()
);
}

// 랜덤으로 섞는다.
private List<PetVideo> shuffleVideos(final Slice<PetVideo> petVideoSlice) {
List<PetVideo> petVideos = new ArrayList<>(petVideoSlice.getContent());
private List<PetVideo> shuffleVideos(final List<PetVideo> petVideos) {
Collections.shuffle(petVideos);
return petVideos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,35 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PetVideoRepository extends JpaRepository<PetVideo, Integer> {

@Query("""
select pv
select pv.id
from PetVideo pv
left join fetch pv.pet p
left join fetch p.shelter s
left join pv.pet p
left join p.shelter s
where s.address.province = :province
and p.type = :petType
order by pv.likeCount desc
""")
Slice<PetVideo> findSliceBy(Pageable pageable);
Slice<Integer> findSliceOfIds(PetType petType, Province province, Pageable pageable);

@Query("""
select pv.id
from PetVideo pv
order by pv.likeCount desc
""")
Slice<Integer> findSliceOfIds(Pageable pageable);

@Query("""
select pv
from PetVideo pv
left join fetch pv.pet p
left join fetch p.shelter s
where s.address.province = :province
and p.type = :petType
where pv.id in :petVideoIds
order by pv.likeCount desc
""")
Slice<PetVideo> findSliceBy(PetType petType, Province province, Pageable searchCondition);
List<PetVideo> findAllByPetVideoIdIn(List<Integer> petVideoIds);
}
1 change: 1 addition & 0 deletions animory/src/main/resources/application-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spring:
hibernate.ddl-auto: validate
properties:
hibernate.format_sql: true
query.fail_on_pagination_over_collection_fetch: true
data.web.pageable.one-indexed-parameters: true # Request로 들어오는 Pageable 의 인덱스를 1부터 시작하도록 설정합니다.
# DB Scheme History Management
flyway:
Expand Down
1 change: 1 addition & 0 deletions animory/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spring:
hibernate.ddl-auto: create
properties:
hibernate.format_sql: true
query.fail_on_pagination_over_collection_fetch: true
flyway.enabled: false # H2 Error
data.web.pageable.one-indexed-parameters: true # Request로 들어오는 Pageable 의 인덱스를 1부터 시작하도록 설정합니다.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
Expand All @@ -23,9 +25,10 @@ class PetVideoRepositoryOrderByTest extends WithTimeSupportObjectMapper {

@Test
void 홈화면_숏폼조회쿼리_테스트() {
Slice<PetVideo> petVideos = petVideoRepository.findSliceBy(PageRequest.of(0, 10));
Slice<Integer> petVideoIds = petVideoRepository.findSliceOfIds(PageRequest.of(0, 10));
List<PetVideo> petVideos = petVideoRepository.findAllByPetVideoIdIn(petVideoIds.getContent());
print(petVideos);

assertThat(petVideos.getContent().get(0).getLikeCount()).isEqualTo(5000);
assertThat(petVideos.get(0).getLikeCount()).isEqualTo(5000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class PetVideoRepositoryTest extends DataJpaTestWithDummyData {
Expand All @@ -18,11 +20,11 @@ class PetVideoRepositoryTest extends DataJpaTestWithDummyData {

@Test
void findSliceBy() {
final Slice<PetVideo> slice = petVideoRepository.findSliceBy(PetType.DOG, Province.광주, PageRequest.of(0, 10));

print(slice.getContent());
final Slice<Integer> petVideoIds = petVideoRepository.findSliceOfIds(PetType.DOG, Province.광주,
PageRequest.of(0, 10));
final List<PetVideo> petVideos = petVideoRepository.findAllByPetVideoIdIn(petVideoIds.getContent());

assertThat(slice.getContent()).hasSize(10);
assertThat(petVideos).hasSize(10);
}


Expand Down

0 comments on commit 21809a8

Please sign in to comment.