Skip to content

Commit

Permalink
Merge pull request #56 from yht0827/feature/#55
Browse files Browse the repository at this point in the history
[#55] 커버링 인덱스로 개선작업
  • Loading branch information
yht0827 authored Oct 28, 2024
2 parents 8851095 + dfcef41 commit 019cde5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.example.kfanboy.global.common.response.PageResponseDto;
import com.example.kfanboy.member.dto.UserResponseDto;
Expand All @@ -30,12 +31,7 @@ public class MemberCustomRepositoryImpl implements MemberCustomRepository {
public PageResponseDto<UserResponseDto> getUserList(final MemberSearchCondition memberSearchCondition,
final Pageable pageable) {

List<UserResponseDto> list = queryFactory.select(Projections.constructor(UserResponseDto.class,
member.memberId,
member.email,
member.nickName,
member.userRole
))
List<Long> ids = queryFactory.select(member.memberId)
.from(member)
.where(
likeNickName(memberSearchCondition.nickName()))
Expand All @@ -44,15 +40,25 @@ public PageResponseDto<UserResponseDto> getUserList(final MemberSearchCondition
.limit(pageable.getPageSize())
.fetch();

if (list.isEmpty()) {
if (CollectionUtils.isEmpty(ids)) {
return PageResponseDto.toDto(Page.empty());
}

List<UserResponseDto> list = queryFactory.select(Projections.constructor(UserResponseDto.class,
member.memberId,
member.email,
member.nickName,
member.userRole
))
.from(member)
.where(member.memberId.in(ids))
.orderBy(member.memberId.desc())
.fetch();

JPAQuery<Long> count = queryFactory
.select(member.count())
.from(member)
.where(
likeNickName(memberSearchCondition.nickName()));
.where(likeNickName(memberSearchCondition.nickName()));

return PageResponseDto.toDto(PageableExecutionUtils.getPage(list, pageable, count::fetchOne));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ public class Vote extends BaseTimeEntity {
@Column(name = "member_id", nullable = false)
private Long memberId;

@Column(name = "nick_name", nullable = false)
private String nickName;

@Column(name = "deleted_at")
private LocalDateTime deletedAt;

@Builder
public Vote(Long voteId, String title, LocalDateTime startAt, LocalDateTime endAt, Boolean isFinished,
Long voteCount, Long maxVoteCount, Long memberId, LocalDateTime deletedAt) {
Long voteCount, Long maxVoteCount, Long memberId, String nickName, LocalDateTime deletedAt) {
this.voteId = voteId;
this.title = title;
this.startAt = startAt;
Expand All @@ -74,6 +77,7 @@ public Vote(Long voteId, String title, LocalDateTime startAt, LocalDateTime endA
this.voteCount = voteCount;
this.maxVoteCount = maxVoteCount;
this.memberId = memberId;
this.nickName = nickName;
this.deletedAt = deletedAt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.example.kfanboy.global.common.response.PageResponseDto;
import com.example.kfanboy.member.dto.UserResponseDto;
Expand Down Expand Up @@ -64,24 +65,29 @@ vote.maxVoteCount, constructor(UserResponseDto.class, member.memberId, member.em
public PageResponseDto<VoteResponseDto> getVoteList(final VoteSearchCondition voteSearchCondition,
final Pageable pageable) {

List<VoteResponseDto> list = jpaQueryFactory.select(
Projections.constructor(VoteResponseDto.class, vote.voteId, vote.title, vote.startAt, vote.endAt,
member.memberId, member.nickName))
List<Long> ids = jpaQueryFactory.select(vote.voteId)
.from(vote)
.innerJoin(member)
.on(vote.memberId.eq(member.memberId))
.where(startWithVoteTitle(voteSearchCondition.title()),
startWithVoteCreatorNickName(voteSearchCondition.nickName()))
.orderBy(vote.voteId.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

if (list.isEmpty()) {
if (CollectionUtils.isEmpty(ids)) {
return PageResponseDto.toDto(Page.empty());
}

JPAQuery<Long> count = jpaQueryFactory.select(vote.count())
List<VoteResponseDto> list = jpaQueryFactory.select(
Projections.constructor(VoteResponseDto.class, vote.voteId, vote.title, vote.startAt, vote.endAt,
vote.memberId, vote.nickName))
.from(vote)
.where(vote.voteId.in(ids))
.orderBy(vote.voteId.desc())
.fetch();

JPAQuery<Long> count = jpaQueryFactory
.select(vote.count())
.from(vote)
.where(startWithVoteTitle(voteSearchCondition.title()),
startWithVoteCreatorNickName(voteSearchCondition.nickName()));
Expand All @@ -90,7 +96,7 @@ public PageResponseDto<VoteResponseDto> getVoteList(final VoteSearchCondition vo
}

private BooleanExpression startWithVoteCreatorNickName(final String keyword) {
return StringUtils.isEmpty(keyword) ? null : member.nickName.startsWith(keyword);
return StringUtils.isEmpty(keyword) ? null : vote.nickName.startsWith(keyword);
}

private BooleanExpression startWithVoteTitle(final String keyword) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/resources/db/migration/V1.4__create_table_vote.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ CREATE table vote
vote_count integer not null,
max_vote_count integer not null,
is_finished boolean not null,
start_at datetime(6) not null default now(6),
end_at datetime(6) default null,
created_at datetime(6) not null default now(6),
updated_at datetime(6) not null default now(6),
deleted_at datetime(6) default null,
start_at datetime(6) not null default now(6),
end_at datetime(6) default null,
created_at datetime(6) not null default now(6),
updated_at datetime(6) not null default now(6),
deleted_at datetime(6) default null,
member_id bigint not null,
nick_name varchar(30) not null,
primary key (vote_id)
);

Expand Down

0 comments on commit 019cde5

Please sign in to comment.