-
Notifications
You must be signed in to change notification settings - Fork 17
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
여행 생성 및 여행 상세 조회 쿼리 개선 #651
Changes from all commits
82186ed
a172e46
70ff2c3
3e9a3be
899b2af
7b6b60c
de40180
4d464e9
b8f9432
033e403
a23f0b9
d3ab7a8
f812570
75c3a7c
1b1a4c6
f5ea739
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,25 @@ | ||
package hanglog.city.domain.repository; | ||
|
||
import hanglog.city.domain.City; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface CityRepository extends JpaRepository<City, Long> { | ||
|
||
@Query(""" | ||
SELECT c | ||
FROM City c | ||
WHERE c.id in :ids | ||
""") | ||
List<City> findCitiesByIds(@Param("ids") final List<Long> ids); | ||
|
||
@Query(""" | ||
SELECT c | ||
FROM City c, TripCity tc | ||
WHERE c.id = tc.city.id | ||
AND tc.trip.id = :tripId | ||
""") | ||
List<City> findCitiesByTripId(@Param("tripId") final Long tripId); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,7 +16,9 @@ | |||||
import jakarta.persistence.OrderBy; | ||||||
import java.time.LocalDate; | ||||||
import java.util.ArrayList; | ||||||
import java.util.HashSet; | ||||||
import java.util.List; | ||||||
import java.util.Set; | ||||||
import lombok.Getter; | ||||||
import lombok.NoArgsConstructor; | ||||||
import org.hibernate.annotations.SQLDelete; | ||||||
|
@@ -47,7 +49,7 @@ public class DayLog extends BaseEntity { | |||||
|
||||||
@OneToMany(mappedBy = "dayLog", cascade = REMOVE, orphanRemoval = true) | ||||||
@OrderBy(value = "ordinal ASC") | ||||||
private List<Item> items = new ArrayList<>(); | ||||||
private Set<Item> items = new HashSet<>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
아이템 순서도 보장하게 LinkedHashSet을 사용하는건 어떤가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 순서는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 내부적으로 LinkedHashSet으로 처리한다면, 어떤 부분에서 성능이 다른가용 ?? 갖고 있을 때 메모리를 더 잡아먹는다 ?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
저도 홍고와 라온 말처럼 명시적으로 혹은 혹시 모를 버그를 방지하기 위한 LinkedHashSet으로 할당하는 것에 한표 하겠습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 성능 차이는 일반 List와 LinkedList의 차이와 같습니다.! 그리고 명시적으로 라는 부분도 잘 모르곘는게, 그런데 DayLog의 Items는 다르다고 생각합니다. 그으으으런데 홍고의 말대로 |
||||||
|
||||||
public DayLog( | ||||||
final Long id, | ||||||
|
@@ -60,7 +62,7 @@ public DayLog( | |||||
this.title = title; | ||||||
this.ordinal = ordinal; | ||||||
this.trip = trip; | ||||||
this.items = items; | ||||||
this.items = new HashSet<>(items); | ||||||
} | ||||||
|
||||||
public DayLog( | ||||||
|
@@ -83,4 +85,12 @@ public LocalDate calculateDate() { | |||||
final LocalDate startDate = trip.getStartDate(); | ||||||
return startDate.plusDays((long) ordinal - DEFAULT_DAY); | ||||||
} | ||||||
|
||||||
public void addItem(final Item item) { | ||||||
items.add(item); | ||||||
} | ||||||
|
||||||
public List<Item> getItems() { | ||||||
return new ArrayList<>(items); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hanglog.trip.domain.repository; | ||
|
||
import hanglog.trip.domain.DayLog; | ||
import java.util.List; | ||
|
||
public interface CustomDayLogRepository { | ||
|
||
void saveAll(final List<DayLog> dayLogs); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package hanglog.trip.domain.repository; | ||
|
||
import hanglog.trip.domain.DayLog; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class CustomDayLogRepositoryImpl implements CustomDayLogRepository { | ||
|
||
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; | ||
|
||
@Override | ||
public void saveAll(final List<DayLog> dayLogs) { | ||
final String sql = """ | ||
INSERT INTO day_log (created_at, modified_at, ordinal, status, title, trip_id) | ||
VALUES (:createdAt, :modifiedAt, :ordinal, :status, :title, :tripId) | ||
"""; | ||
namedParameterJdbcTemplate.batchUpdate(sql, getDayLogToSqlParameterSources(dayLogs)); | ||
} | ||
|
||
private MapSqlParameterSource[] getDayLogToSqlParameterSources(final List<DayLog> dayLogs) { | ||
return dayLogs.stream() | ||
.map(this::getDayLogToSqlParameterSource) | ||
.toArray(MapSqlParameterSource[]::new); | ||
} | ||
|
||
private MapSqlParameterSource getDayLogToSqlParameterSource(final DayLog dayLog) { | ||
final LocalDateTime now = LocalDateTime.now(); | ||
return new MapSqlParameterSource() | ||
.addValue("createdAt", now) | ||
.addValue("modifiedAt", now) | ||
.addValue("ordinal", dayLog.getOrdinal()) | ||
.addValue("status", dayLog.getStatus().name()) | ||
.addValue("title", dayLog.getTitle()) | ||
.addValue("tripId", dayLog.getTrip().getId()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hanglog.trip.domain.repository; | ||
|
||
import hanglog.city.domain.City; | ||
import java.util.List; | ||
|
||
public interface CustomTripCityRepository { | ||
|
||
void saveAll(final List<City> cities, final Long tripId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hanglog.trip.domain.repository; | ||
|
||
import static hanglog.global.type.StatusType.USABLE; | ||
|
||
import hanglog.city.domain.City; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class CustomTripCityRepositoryImpl implements CustomTripCityRepository { | ||
|
||
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; | ||
|
||
@Override | ||
public void saveAll(final List<City> cities, final Long tripId) { | ||
final String sql = """ | ||
INSERT INTO trip_city (city_id, created_at, modified_at, status, trip_id) | ||
VALUES (:cityId, :createdAt, :modifiedAt, :status, :tripId) | ||
"""; | ||
namedParameterJdbcTemplate.batchUpdate(sql, getTripCityToSqlParameterSources(cities, tripId)); | ||
} | ||
|
||
private MapSqlParameterSource[] getTripCityToSqlParameterSources(final List<City> cities, final Long tripId) { | ||
return cities.stream() | ||
.map(city -> getTripCityToSqlParameterSource(city, tripId)) | ||
.toArray(MapSqlParameterSource[]::new); | ||
} | ||
|
||
private MapSqlParameterSource getTripCityToSqlParameterSource(final City city, final Long tripId) { | ||
final LocalDateTime now = LocalDateTime.now(); | ||
return new MapSqlParameterSource() | ||
.addValue("cityId", city.getId()) | ||
.addValue("createdAt", now) | ||
.addValue("modifiedAt", now) | ||
.addValue("status", USABLE.name()) | ||
.addValue("tripId", tripId); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 ㅋㅋㅋ 👍 👍 👍 👍 👍