Skip to content

Commit

Permalink
fix: N+1 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
amaran-th committed Nov 8, 2023
1 parent 9998b29 commit 0d180a6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ public class Member extends BaseEntity {
private String githubUsername;

public Member(final Long id, final Long githubId, final String imageUrl, final String name,
final String description,
final String githubUsername) {
this.id = id;
this.githubId = githubId;
this.imageUrl = imageUrl;
this.name = name;
this.description = DEFAULT_DESCRIPTION;
this.description = description;
this.githubUsername = githubUsername;
}

public Member(final Long id, final Long githubId, final String imageUrl, final String name,
final String githubUsername) {
this(id, githubId, imageUrl, name, DEFAULT_DESCRIPTION, githubUsername);
}

public Member(final Long githubId, final String imageUrl, final String githubUsername) {
this.githubId = githubId;
this.imageUrl = imageUrl;
this.description = DEFAULT_DESCRIPTION;
this.githubUsername = githubUsername;
this(null, githubId, imageUrl, null, DEFAULT_DESCRIPTION, githubUsername);
}

public void updateName(final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public List<RoomResponse> findAll(final Member loginMember, final Long memberId)
.map(messageOverview ->
RoomResponse.from(
messageOverview,
memberRepository.findById(messageOverview.getSenderId())
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER)),
// TODO: 2023/11/05 N+1 문제
findInterlocutor(interlocutorIdPerRoomExceptMe.get(messageOverview.getRoomUUID()))
))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class RoomResponse {

public static RoomResponse from(
final MessageOverview messageOverview,
final Member recentMessageSender,
final Member interlocutor
) {
return new RoomResponse(
Expand All @@ -26,7 +25,7 @@ public static RoomResponse from(
MessageResponse.from(new Message(
messageOverview.getId(),
messageOverview.getContent(),
recentMessageSender,
messageOverview.getSender(),
messageOverview.getRoomUUID(),
messageOverview.getCreatedAt()))
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.emmsale.message_room.infrastructure.persistence;


import com.emmsale.member.domain.Member;
import com.emmsale.message_room.infrastructure.persistence.dto.MessageOverview;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,9 +15,16 @@ public class MessageDao {

private static final RowMapper<MessageOverview> ROW_MAPPER = (rs, rowNum) ->
new MessageOverview(
rs.getLong("id"),
rs.getLong("m2.id"),
rs.getString("content"),
rs.getLong("sender_id"),
new Member(
rs.getLong("sender.id"),
rs.getLong("github_id"),
rs.getString("image_url"),
rs.getString("name"),
rs.getString("description"),
rs.getString("github_username")
),
rs.getTimestamp("created_at").toLocalDateTime(),
rs.getString("room_id")
);
Expand All @@ -27,12 +35,15 @@ public List<MessageOverview> findRecentlyMessages(final Long memberId) {
StringBuilder sqlBuilder = new StringBuilder();

sqlBuilder
.append("SELECT m2.id, m2.content, m2.sender_id, m2.created_at, m2.room_id ")
.append("SELECT m2.id, m2.content, m2.sender_id, m2.created_at, m2.room_id, ")
.append(
"sender.id, sender.github_id, sender.image_url, sender.name, sender.description, sender.github_username ")
.append("FROM room r ")
.append("JOIN (SELECT room_id, MAX(created_at) as max_created_at ")
.append(" FROM message ")
.append(" GROUP BY room_id) m1 ON r.uuid = m1.room_id ")
.append("JOIN message m2 ON m1.room_id = m2.room_id AND m1.max_created_at = m2.created_at ")
.append("JOIN member sender ON m2.sender_id = sender.id ")
.append("WHERE r.member_id = ? ")
.append("AND m2.created_at > r.last_exited_time ")
.append("ORDER BY m2.created_at DESC");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.emmsale.message_room.infrastructure.persistence.dto;

import com.emmsale.member.domain.Member;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -10,7 +11,7 @@ public class MessageOverview {

private final Long id;
private final String content;
private final Long senderId;
private final Member sender;
private final LocalDateTime createdAt;
private final String roomUUID;
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,23 @@ void test_findAll() throws Exception {
final MessageOverview messageOverview1 = new MessageOverview(
resultMessage1.getId(),
resultMessage1.getContent(),
resultMessage1.getSender().getId(),
resultMessage1.getSender(),
resultMessage1.getCreatedAt(),
resultMessage1.getRoomId()
);

final MessageOverview messageOverview2 = new MessageOverview(
resultMessage2.getId(),
resultMessage2.getContent(),
resultMessage2.getSender().getId(),
resultMessage2.getSender(),
resultMessage2.getCreatedAt(),
resultMessage2.getRoomId()
);

final List<RoomResponse> expect = List.of(
RoomResponse.from(messageOverview1, resultMessage1.getSender(),
RoomResponse.from(messageOverview1,
memberRepository.findById(room1Interlocutor.getId()).get()),
RoomResponse.from(messageOverview2, resultMessage2.getSender(),
RoomResponse.from(messageOverview2,
memberRepository.findById(room2Interlocutor.getId()).get())
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ void test_findRecentlyMessages() throws Exception {
new MessageOverview(
resultMessage1.getId(),
resultMessage1.getContent(),
resultMessage1.getSender().getId(),
resultMessage1.getSender(),
resultMessage1.getCreatedAt(),
resultMessage1.getRoomId()
),
new MessageOverview(
resultMessage2.getId(),
resultMessage2.getContent(),
resultMessage2.getSender().getId(),
resultMessage2.getSender(),
resultMessage2.getCreatedAt(),
resultMessage2.getRoomId()
)
Expand All @@ -128,6 +128,7 @@ void test_findRecentlyMessages() throws Exception {
//then
Assertions.assertThat(actual)
.usingRecursiveComparison()
.ignoringFields("sender.createdAt", "sender.updatedAt")
.isEqualTo(expect);
}
}

0 comments on commit 0d180a6

Please sign in to comment.