Skip to content

Commit

Permalink
#547 [feat] n+1 쿼리 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
sohyundoh committed Oct 27, 2024
1 parent cf3c2e4 commit 161cd4a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public List<PostAndCuriousCountInLastWeek> findMostCuriousPostBeforeOneWeek(fina
.join(curious.post, post)
.join(topic).on(post.topic.id.eq(topic.id))
.join(moim).on(topic.moim.id.eq(moim.id))
.join(post.writerName).fetchJoin()
.where(moim.id.eq(targetMoim.getId()))
.where(curious.createdAt.between(now.minusDays(WEEK), now))
.groupBy(post.id)
Expand All @@ -41,6 +42,7 @@ public List<Post> findPostByLatestCurious(final Moim targetMoim, final int reque
.join(curious.post, post)
.join(topic).on(post.topic.id.eq(topic.id))
.join(moim).on(topic.moim.id.eq(moim.id))
.join(post.writerName).fetchJoin()
.where(moim.id.eq(targetMoim.getId()))
.where(post.notIn(posts))
.orderBy(curious.createdAt.desc())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mile.moim.domain.popular.MoimPopularInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.Repository;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import java.util.Optional;

public interface MoimRepository extends JpaRepository<Moim, Long> {

@Query("select m from Moim m join fetch m.owner where m.id = :id")
Optional<Moim> findById(final long id);

Boolean existsByNormalizedName(final String normalizedName);

@Query("SELECT m FROM Post p JOIN p.topic t JOIN t.moim m WHERE m.isPublic = true AND p.isTemporary = false AND p.createdAt BETWEEN :startOfWeek AND :endOfWeek GROUP BY m ORDER BY COUNT(p)")
Expand Down

0 comments on commit 161cd4a

Please sign in to comment.