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

[refactor]: 조회성능을 향상시키기 위하여 places에 인덱스를 추가했어요 #162

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Page<PlaceInfo> getPlacesWithinRadius(
List<PlaceInfo> placeInfos = convertToPlaceInfos(placesPage, placeIdToInfluencerName);

// PlaceInfo 리스트를 Page로 변환하여 반환
return new PageImpl<>(placeInfos, placesPage.getPageable(), placeInfos.size());
return new PageImpl<>(placeInfos, placesPage.getPageable(), placesPage.getTotalElements());
}

private List<PlaceInfo> convertToPlaceInfos(Page<Place> placesPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Page<Place> findPlacesByDistanceAndFilters(String topLeftLongitude,
.fetch();

// 2. countQuery를 따로 선언하여 필요할 때만 실행
JPAQuery<Long> countQuery = jpaQueryFactory.select(place.count())
JPAQuery<Long> countQuery = jpaQueryFactory.select(place.id.count()) // 중복 제거
.from(place)
.leftJoin(video).on(video.place.eq(place))
.leftJoin(influencer).on(video.influencer.eq(influencer))
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ create table places
latitude text not null,
longitude text not null,
menu_img_url text null,
category enum ('CAFE', 'JAPANESE', 'KOREAN', 'NONE', 'RESTAURANT', 'WESTERN') not null
category enum ('CAFE', 'JAPANESE', 'KOREAN', 'NONE', 'RESTAURANT', 'WESTERN') not null,
index idx_long_lat (longitude(15), latitude(15))
);

create table places_menuboardphotourl_list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public void init() {

entityManager.persist(video1);
entityManager.persist(video2);

pageable = PageRequest.of(0, 10);
}

@Test
Expand Down Expand Up @@ -178,6 +180,7 @@ public void test2() {
influencers,
pageable
);
assertThat(foundPlaces.getTotalElements()).isEqualTo(3);
// Then
assertThat(foundPlaces).hasSize(3);
assertThat(foundPlaces.getContent().get(0).getName()).isEqualTo("Place 2");
Expand Down Expand Up @@ -317,4 +320,38 @@ public void test7() {
assertThat(foundPlaces).hasSize(1);
assertThat(foundPlaces.getContent().get(0).getName()).isEqualTo("Place 4");
}

// Pagenation
@Test
@DisplayName("page=1&size=2, 필터링 NULL, boundary[(10, 60), (50, 10)]")
public void test8() {

// * Place 3(10.0, 100.0) 제외
// * Place 2, Place 4는 page=0이라 제외, totalPages, totalElements에는 포함됨
// given
List<String> categories = null;
List<String> influencers = null;
pageable = PageRequest.of(1, 2);

// when
Page<Place> foundPlaces = placeRepository.findPlacesByDistanceAndFilters(
topLeftLongitude,
topLeftLatitude,
bottomRightLongitude,
bottomRightLatitude,
longitude,
latitude,
categories,
influencers,
pageable
);
// Then
// pagenation
assertThat(foundPlaces.getTotalElements()).isEqualTo(3);
assertThat(foundPlaces.getTotalPages()).isEqualTo(2);
assertThat(foundPlaces).hasSize(1);
// places
assertThat(foundPlaces.getContent().get(0).getName()).isEqualTo("Place 1");

}
}