Query Performance Comparison
쿼리 성능 비교
상황 및 문제
해결방법
방법1: 전체 피드 컬렉션을 모두 조회 후 각 컬렉션의 무드를 가져온다.
예시
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Immutable
@Subselect("select DISTINCT _feedCollection.id as id " +
", _feedCollection.author_id as author_id " +
", _member.nickname as author_nickname " +
", _taste_mood.name as author_mood " +
", _image.url as author_thumbnail_url " +
", _feedCollection.title as title " +
", _feedCollection.description as description " +
", _like_count.count as like_count " +
", _feedCollection.follower_count as follower_count " +
", _feedCollection.thumbnail_url as thumbnail_url " +
", _feedCollection.is_private as is_private " +
", (select count(id) from feed_collection_feed_ids where feed_collection_feed_ids.id = _feedCollection.id ) as feed_count " +
", (select count(id) from feed_collection_comment_ids where _feedCollectionCommentIds.comment_id = _feedCollection.id) as comment_count " +
", false as liked " +
", _feedCollection.moods_id as moods_id " +
", _feedCollection.created_at as created_at " +
", _feedCollection.updated_at as updated_at " +
"FROM feed_collection _feedCollection " +
"JOIN member _member on _feedCollection.author_id = _member.id " +
"JOIN image _image on _member.profile_image_id = _image.id " +
"JOIN taste_mood _taste_mood on _member.taste_mood_id = _taste_mood.id " +
"LEFT JOIN feed_collection_like_count _like_count on _feedCollection.id = _like_count.feed_collection_id " +
"LEFT JOIN feed_collection_feed_ids as _feedCollecitonIds on _feedCollection.id = _feedCollecitonIds.feed_id " +
"LEFT JOIN feed_collection_comment_ids as _feedCollectionCommentIds on _feedCollection.id = _feedCollectionCommentIds.comment_id "
)
@Table(name = "feed_collection")
public class FeedCollectionSample {
@Id
private FeedCollectionId id;
@AttributeOverride(name = "value", column = @Column(name = "author_id"))
private MemberId authorId;
private String authorNickname;
private String authorMood;
private String authorThumbnailUrl;
private String title;
private String description;
private int likeCount;
private int followerCount;
private String thumbnailUrl;
private boolean isPrivate;
private int feedCount;
private int commentCount;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "moods_id")
private FeedCollectionMoods moods;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}방법2: 하나의 쿼리로 모든 데이터를 가져온 다음 비지니스 로직에서 grouping하고 translate한다.
예시
의문?
테스트
조건
문제 1


해결방법
index 추가 검토
FeedCollection 정렬 Query

Member 테이블 조인

Image 테이블 조인

TasteMood 테이블 조인

FeedCollectionLikeCount 테이블 조인

FeedCollectionMoods 테이블 조인

FeedCollectionMoodsMoodList 테이블 조인

FeedCollectionMood 테이블 조인

FeedCollectionFeedIds 테이블 서브 쿼리

FeedCollectionCommentIds 테이블 서버 쿼리

FeedCollectionLike 테이블 서버 쿼리



index 추가 후(Explain)


문제 2
기존 Subselect 조회가 안됨(시간이 너무 오래 걸림...)
분석


해결방법


성능 비교
방법1: 전체 피드 컬렉션을 모두 조회 후 각 컬렉션의 무드를 가져온다.
실행시간
방법2: 하나의 쿼리로 모든 데이터를 가져온 다음 비지니스 로직에서 grouping하고 translate한다.
실행시간
결론
대량 데이터를 조회시 index 설계는 반드시 고려해야 할 사항이다
함부로 subselect를 사용하면 안된다
단일 쿼리로 모든 데이터를 가져오는 것과 제한된 여러 쿼리로 데이터를 나눠서 가져오는 것 사이에는 속도 면에서 큰 차이는 없다
Last updated