Skip to content

Commit fd7fe2b

Browse files
committed
♻️ refactor: userId 기반 단건 조회 쿼리 메서드로 성능 개선
1 parent e81b2ce commit fd7fe2b

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

backend/src/main/java/io/f1/backend/domain/stat/dao/StatJpaRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,14 @@ public interface StatJpaRepository extends JpaRepository<Stat, Long> {
6363
s.user.id = :userId
6464
""")
6565
void updateStatByUserIdCaseLose(long deltaScore, long userId);
66+
67+
@Query(
68+
"""
69+
SELECT new io.f1.backend.domain.stat.dto.StatWithNicknameAndUserId(
70+
u.id, u.nickname, s.totalGames, s.winningGames, s.score
71+
)
72+
FROM Stat s JOIN s.user u
73+
WHERE u.id = :userId
74+
""")
75+
Optional<StatWithNicknameAndUserId> findByUserId(long userId);
6676
}

backend/src/main/java/io/f1/backend/domain/stat/dao/StatRepositoryAdapter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,15 @@ public MyPageInfo getMyPageByUserId(long userId) {
108108
log.error("Redis miss, fallback to MySQL for userId={}", userId, e);
109109
}
110110

111-
StatWithNicknameAndUserId stat = findFirstMatchingStat(userId);
111+
StatWithNicknameAndUserId stat = findStatByUserId(userId);
112112
long rank = jpaRepository.countByScoreGreaterThan(stat.score()) + 1;
113113

114114
return new MyPageInfo(
115115
stat.nickname(), rank, stat.totalGames(), stat.winningGames(), stat.score());
116116
}
117117

118-
private StatWithNicknameAndUserId findFirstMatchingStat(long userId) {
119-
return jpaRepository.findAllStatWithNicknameAndUserId().stream()
120-
.filter(stat -> stat.userId() == userId)
121-
.findFirst()
122-
.orElseThrow(() -> new CustomException(UserErrorCode.USER_NOT_FOUND));
118+
private StatWithNicknameAndUserId findStatByUserId(long userId) {
119+
return jpaRepository.findByUserId(userId)
120+
.orElseThrow(() -> new CustomException(UserErrorCode.USER_NOT_FOUND));
123121
}
124122
}

0 commit comments

Comments
 (0)