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: 팔로잉 소식 조회 쿼리 성능 개선 #437

Merged
merged 6 commits into from
Sep 23, 2024
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.depromeet.followinglog.repository;

import static com.depromeet.followinglog.entity.QFollowingMemoryLogEntity.followingMemoryLogEntity;
import static com.depromeet.friend.entity.QFriendEntity.friendEntity;
import static com.depromeet.member.entity.QMemberEntity.memberEntity;
import static com.depromeet.memory.entity.QMemoryEntity.memoryEntity;
import static com.depromeet.memory.entity.QStrokeEntity.strokeEntity;

import com.depromeet.followinglog.domain.FollowingMemoryLog;
import com.depromeet.followinglog.entity.FollowingMemoryLogEntity;
import com.depromeet.followinglog.entity.QFollowingMemoryLogEntity;
import com.depromeet.followinglog.port.out.persistence.FollowingMemoryLogPersistencePort;
import com.depromeet.friend.entity.QFriendEntity;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
Expand All @@ -21,9 +20,6 @@ public class FollowingMemoryLogRepository implements FollowingMemoryLogPersisten
private final JPAQueryFactory queryFactory;
private final FollowingMemoryLogJpaRepository followingMemoryLogJpaRepository;

private QFollowingMemoryLogEntity followingMemoryLog =
QFollowingMemoryLogEntity.followingMemoryLogEntity;

Comment on lines -24 to -26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 방식은 왜 바꾼 건가요??

Copy link
Collaborator Author

@penrose15 penrose15 Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 편이 더 깔끔해 보여서 없애봤습니다. 다음 PR 에서 다른 Repository도 동일하게 적용해볼까 생각하고 있습니다.

@Override
public Long save(FollowingMemoryLog followingMemoryLog) {
return followingMemoryLogJpaRepository
Expand All @@ -33,66 +29,53 @@ public Long save(FollowingMemoryLog followingMemoryLog) {

@Override
public List<FollowingMemoryLog> findLogsByMemberIdAndCursorId(Long memberId, Long cursorId) {
QFriendEntity friend = QFriendEntity.friendEntity;

List<FollowingMemoryLogEntity> contents =
queryFactory
.selectFrom(followingMemoryLog)
.join(followingMemoryLog.memory, memoryEntity)
.fetchJoin()
.join(followingMemoryLog.memory.member, memberEntity)
.selectFrom(followingMemoryLogEntity)
.join(followingMemoryLogEntity.memory, memoryEntity)
.fetchJoin()
.leftJoin(followingMemoryLog.memory.memoryDetail)
.join(memoryEntity.member, memberEntity)
.fetchJoin()
.leftJoin(followingMemoryLog.memory.strokes, strokeEntity)
.leftJoin(memoryEntity.memoryDetail)
.fetchJoin()
.join(friend)
.on(friend.following.eq(memberEntity))
.join(friendEntity)
.on(friendEntity.following.eq(memberEntity))
.fetchJoin()
.where(friend.member.id.eq(memberId), cursorIdLt(cursorId))
.where(friendEntity.member.id.eq(memberId), cursorIdLt(cursorId))
.limit(11)
.orderBy(followingMemoryLog.id.desc())
.orderBy(followingMemoryLogEntity.id.desc())
.fetch();
queryFactory
.selectFrom(followingMemoryLog)
.join(followingMemoryLog.memory, memoryEntity)
.fetchJoin()
.join(followingMemoryLog.memory.member, memberEntity)
.fetchJoin()
.leftJoin(followingMemoryLog.memory.images)
.fetchJoin()
.fetch();
penrose15 marked this conversation as resolved.
Show resolved Hide resolved
return contents.stream().map(FollowingMemoryLogEntity::toModel).toList();
}

@Override
public void deleteAllByMemoryIds(List<Long> memoryIds) {
queryFactory.delete(followingMemoryLog).where(memoryIdIn(memoryIds)).execute();
queryFactory.delete(followingMemoryLogEntity).where(memoryIdIn(memoryIds)).execute();
}

@Override
public void deleteAllByMemoryId(Long memoryId) {
queryFactory.delete(followingMemoryLog).where(memoryIdEq(memoryId)).execute();
queryFactory.delete(followingMemoryLogEntity).where(memoryIdEq(memoryId)).execute();
}

private BooleanExpression memoryIdIn(List<Long> memoryIds) {
if (memoryIds == null) {
return null;
}
return followingMemoryLog.memory.id.in(memoryIds);
return followingMemoryLogEntity.memory.id.in(memoryIds);
}

private BooleanExpression memoryIdEq(Long memoryId) {
if (memoryId == null) {
return null;
}
return followingMemoryLog.memory.id.eq(memoryId);
return followingMemoryLogEntity.memory.id.eq(memoryId);
}

private BooleanExpression cursorIdLt(Long cursorId) {
if (cursorId == null) {
return null;
}
return followingMemoryLog.id.lt(cursorId);
return followingMemoryLogEntity.id.lt(cursorId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public boolean addOrDeleteFollow(Long memberId, FollowRequest followRequest) {
}

boolean isAdd = followUseCase.addOrDeleteFollow(member, following);
eventPublisher.publishEvent(FollowLogEvent.of(following, member));
if (isAdd) {
eventPublisher.publishEvent(FollowLogEvent.of(following, member));
}

return isAdd;
}
Expand Down
Loading