Skip to content

Commit

Permalink
🔨 [Refactoring] agenda api 쿼리 조회 성능 개선 #986 (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
yhames authored Sep 2, 2024
1 parent b6da492 commit 78453f3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public List<CurrentAttendAgendaListResDto> findCurrentAttendAgenda(String intraI
AgendaProfile agendaProfile = agendaProfileRepository.findByIntraId(intraId)
.orElseThrow(() -> new NotExistException(AGENDA_PROFILE_NOT_FOUND));

List<AgendaTeamProfile> agendaTeamProfiles = agendaTeamProfileRepository.findByProfileAndIsExistTrue(
agendaProfile);
List<AgendaTeamProfile> agendaTeamProfiles = agendaTeamProfileRepository
.findByProfileAndIsExistTrue(agendaProfile);

return agendaTeamProfiles.stream()
.filter(agendaTeamProfile -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public AgendaTeam confirmTeam(UserDto user, Agenda agenda, UUID teamKey) {
*/
@Transactional(readOnly = true)
public AgendaTeam getAgendaTeam(UUID teamKey) {
return agendaTeamRepository.findByTeamKeyFetchJoin(teamKey)
return agendaTeamRepository.findByTeamKey(teamKey)
.orElseThrow(() -> new NotExistException(AGENDA_TEAM_NOT_FOUND));
}

Expand Down Expand Up @@ -286,7 +286,8 @@ public void modifyAgendaTeam(UserDto user, TeamUpdateReqDto teamUpdateReqDto, UU
throw new ForbiddenException(TEAM_LEADER_FORBIDDEN);
}

List<AgendaTeamProfile> profiles = agendaTeamProfileRepository.findAllByAgendaTeam(agendaTeam);
List<AgendaTeamProfile> profiles = agendaTeamProfileRepository
.findAllByAgendaTeamAndIsExistTrue(agendaTeam);

agenda.updateTeam(Location.valueOfLocation(teamUpdateReqDto.getTeamLocation()), LocalDateTime.now());
agendaTeam.updateTeam(teamUpdateReqDto.getTeamName(), teamUpdateReqDto.getTeamContent(),
Expand Down
3 changes: 2 additions & 1 deletion gg-data/src/main/java/gg/data/agenda/AgendaTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
Expand Down Expand Up @@ -39,7 +40,7 @@ public class AgendaTeam extends BaseTimeEntity {
@Column(name = "id", nullable = false)
private Long id;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "agenda_id", nullable = false)
private Agenda agenda;

Expand Down
2 changes: 0 additions & 2 deletions gg-repo/src/main/java/gg/repo/agenda/AgendaRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public interface AgendaRepository extends JpaRepository<Agenda, Long> {
@Query("SELECT a FROM Agenda a WHERE a.status = :status1 OR a.status = :status2")
Page<Agenda> findAllByStatusIs(AgendaStatus status1, AgendaStatus status2, Pageable pageable);

Optional<Agenda> findAgendaByAgendaKey(UUID usedTo);

@Query("SELECT a FROM Agenda a WHERE a.hostIntraId = :intraId AND (a.status = :status1 OR a.status = :status2)")
Page<Agenda> findAllByHostIntraIdAndStatus(
String intraId, AgendaStatus status1, AgendaStatus status2, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,39 @@
import gg.data.agenda.type.AgendaStatus;

public interface AgendaTeamProfileRepository extends JpaRepository<AgendaTeamProfile, Long> {
@Query("SELECT atp FROM AgendaTeamProfile atp WHERE atp.agendaTeam.agenda = :agenda "
+ "AND atp.profile = :agendaProfile AND atp.isExist = true")
@Query("SELECT atp FROM AgendaTeamProfile atp "
+ "WHERE atp.agendaTeam.agenda = :agenda AND atp.profile = :agendaProfile AND atp.isExist = true")
Optional<AgendaTeamProfile> findByAgendaAndAgendaProfileAndIsExistTrue(Agenda agenda, AgendaProfile agendaProfile);

@Query("SELECT atp FROM AgendaTeamProfile atp WHERE atp.agendaTeam = :agendaTeam AND atp.isExist = true")
@Query("SELECT atp FROM AgendaTeamProfile atp "
+ "WHERE atp.agendaTeam = :agendaTeam AND atp.isExist = true")
List<AgendaTeamProfile> findByAgendaTeamAndIsExistTrue(AgendaTeam agendaTeam);

@Query("SELECT atp FROM AgendaTeamProfile atp WHERE atp.agenda = :agenda AND atp.profile = :agendaProfile "
+ "AND atp.isExist = true")
@Query("SELECT atp FROM AgendaTeamProfile atp "
+ "WHERE atp.agenda = :agenda AND atp.profile = :agendaProfile AND atp.isExist = true")
Optional<AgendaTeamProfile> findByAgendaAndProfileAndIsExistTrue(Agenda agenda, AgendaProfile agendaProfile);

/**
* 해당 메서드는 N+1 문제가 발생할 수 있습니다.
*/
List<AgendaTeamProfile> findAllByAgendaTeam(AgendaTeam agendaTeam);
@Query("SELECT atp FROM AgendaTeamProfile atp JOIN FETCH atp.profile "
+ "WHERE atp.agendaTeam = :agendaTeam AND atp.isExist = true")
List<AgendaTeamProfile> findAllByAgendaTeamAndIsExistTrue(AgendaTeam agendaTeam);

@Query("SELECT atp FROM AgendaTeamProfile atp JOIN FETCH atp.profile WHERE atp.agendaTeam = :agendaTeam")
List<AgendaTeamProfile> findAllByAgendaTeamWithFetchProfile(AgendaTeam agendaTeam);

@Query("SELECT atp FROM AgendaTeamProfile atp WHERE atp.profile = :agendaProfile AND atp.isExist = true")
@Query("SELECT atp FROM AgendaTeamProfile atp JOIN FETCH atp.agenda "
+ "WHERE atp.profile = :agendaProfile AND atp.isExist = true")
List<AgendaTeamProfile> findByProfileAndIsExistTrue(@Param("agendaProfile") AgendaProfile agendaProfile);

@Query(
"SELECT atp FROM AgendaTeamProfile atp "
+ "WHERE atp.profile = :agendaProfile "
+ "AND atp.isExist = true "
+ "AND atp.agenda.status = :status"
)
value = "SELECT atp FROM AgendaTeamProfile atp JOIN FETCH atp.agendaTeam at "
+ "WHERE atp.profile = :agendaProfile AND atp.isExist = true AND atp.agenda.status = :status",
countQuery = "SELECT count(atp) FROM AgendaTeamProfile atp "
+ "WHERE atp.profile = :agendaProfile AND atp.isExist = true AND atp.agenda.status = :status")
Page<AgendaTeamProfile> findByProfileAndIsExistTrueAndAgendaStatus(
@Param("agendaProfile") AgendaProfile agendaProfile,
@Param("status") AgendaStatus status, Pageable pageable);
AgendaProfile agendaProfile, AgendaStatus status, Pageable pageable);

@Query("SELECT atp FROM AgendaTeamProfile atp JOIN FETCH atp.profile "
+ "WHERE atp.agenda = :agenda AND atp.isExist = true")
List<AgendaTeamProfile> findAllByAgendaAndIsExistTrue(Agenda agenda);

@Query("SELECT atp FROM AgendaTeamProfile atp WHERE atp.agendaTeam IN :agendaTeams AND atp.isExist = true")
@Query("SELECT atp FROM AgendaTeamProfile atp JOIN FETCH atp.profile "
+ "WHERE atp.agendaTeam IN :agendaTeams AND atp.isExist = true")
List<AgendaTeamProfile> findByAgendaTeamInAndIsExistTrue(List<AgendaTeam> agendaTeams);
}
11 changes: 1 addition & 10 deletions gg-repo/src/main/java/gg/repo/agenda/AgendaTeamRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ Optional<AgendaTeam> findByAgendaAndTeamNameAndStatus(Agenda agenda, String team
Optional<AgendaTeam> findByAgendaAndTeamKeyAndStatus(Agenda agenda, UUID teamKey, AgendaTeamStatus status1,
AgendaTeamStatus status2);

@Query("SELECT a FROM AgendaTeam a WHERE a.teamKey = :teamKey")
Optional<AgendaTeam> findByTeamKey(UUID teamKey);

@Query("SELECT a FROM AgendaTeam a JOIN FETCH a.agenda WHERE a.teamKey = :teamKey")
Optional<AgendaTeam> findByTeamKeyFetchJoin(UUID teamKey);

@Query("SELECT a FROM AgendaTeam a WHERE a.agenda = :agenda AND a.name = :name AND a.status = :status")
Optional<AgendaTeam> findByAgendaAndNameAndStatus(Agenda agenda, String name, AgendaTeamStatus status);
Optional<AgendaTeam> findByTeamKey(UUID teamKey);

@Query("SELECT a FROM AgendaTeam a WHERE a.agenda = :agenda AND a.status = :status")
List<AgendaTeam> findAllByAgendaAndStatus(Agenda agenda, AgendaTeamStatus status);
Expand All @@ -41,7 +35,4 @@ Optional<AgendaTeam> findByAgendaAndTeamKeyAndStatus(Agenda agenda, UUID teamKey

@Query("SELECT a FROM AgendaTeam a WHERE a.agenda = :agenda AND a.status = :status AND a.isPrivate = false")
Page<AgendaTeam> findByAgendaAndStatusAndIsPrivateFalse(Agenda agenda, AgendaTeamStatus status, Pageable pageable);

@Query("SELECT a FROM AgendaTeam a WHERE a.agenda = :agenda AND a.status = :status")
Page<AgendaTeam> findByAgendaAndStatus(Agenda agenda, AgendaTeamStatus status, Pageable pageable);
}

0 comments on commit 78453f3

Please sign in to comment.