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

Ignore: ✨가입한 채팅방 목록 조회 API #174

Merged
merged 9 commits into from
Oct 14, 2024

Conversation

psychology50
Copy link
Member

@psychology50 psychology50 commented Oct 14, 2024

작업 이유

  • 사용자가 가입한 채팅방 목록 조회 API 구현

작업 사항

  • 요청 url: GET /v2/chat-rooms

-- 1. user_id로 사용자가 가입한 채팅방 리스트 조회
SELECT *
FROM chat_member
WHERE user_id=10;

-- 2. chat_room_id로 채팅방 정보 조회
SELECT *
FROM chat_room cr
WHERE cr.id in (1, 2)
;

-- 3. chat_room 별 참여자 인원 조회
SELECT COUNT(id)
FROM chat_member
WHERE chat_room_id in (1, 2)
GROUP BY chat_room_id;

사용자 -⋲ 채팅방 멤버 ⋺- 채팅방 관계에서, user_id만으로 "내가 가입한 모든 채팅방 목록과 각 채팅방의 참여자 수"를 구하기 위해선 3가지 방법이 있습니다.

  1. 반복문: 사용자가 가입한 채팅방 리스트 조회(1) + 각 채팅방 정보 조회(N) + 각 채팅방 참여자 수 조회(N)
  2. in 키워드: 사용자가 가입한 채팅방 리스트 조회(1) + 각 채팅방 정보 조회(1) + 각 채팅방 참여자 수 조회(1)
  3. sub query: 전체 1회

SELECT 
    cr.id,
    cr.title,
    cr.password,
    cr.created_at,
    cr.updated_at,
    (
         SELECT COUNT(*) 
         FROM chat_member cm 
         WHERE cm.chat_room_id = cr.id
    )
FROM chat_room cr
WHERE cr.id IN (
    SELECT chat_room_id
    FROM chat_member
    WHERE user_id = 10
);

join을 쓰지 않으면서도, 전체 결과를 요청 한 번만에 수신할 수 있는 (3)의 방법을 택하여 구현했습니다.

실제 Hibernate 환경에서 실행되는 쿼리 로그는 아래 첨부합니다.

Details

select 
    cr1_0.id,
    cr1_0.title,
    cr1_0.description,
    cr1_0.background_image_url,
    cr1_0.password,
    cr1_0.created_at,
    (
        select cast(count(cm1_0.id) as signed) 
        from chat_member cm1_0 
        where (cm1_0.deleted_at IS NULL) and cm1_0.chat_room_id=cr1_0.id
     ) 
from chat_room cr1_0 
where (cr1_0.deleted_at IS NULL) 
and cr1_0.id in 
    (
        select cm2_0.chat_room_id 
        from chat_member cm2_0 
        where (cm2_0.deleted_at IS NULL) 
        and cm2_0.user_id=?
    )


리뷰어가 중점적으로 확인해야 하는 부분

  • 더 효율적인 방법이 존재하는지?

발견한 이슈

이슈라기 보단, 고민해봐야 할 내용

  • 최종 채팅방 목록을 정렬을 하려다 수행하지 않음. 왜냐하면, 사용자에게 채팅방 순서는 가장 최근 활동 이력이 있는 채팅방 순서가 실시간으로 최우선 순위로 올라야 하기 때문.
  • 이는 서버에서 제어해줄 영역이 아니라고 판단하지만, 가장 처음 데이터를 초기화하는 단계에서는 정렬 힌트를 클라이언트 측에 제공해줄 수 있어야 할 거 같음. (그걸 어케 하는데??)

@psychology50 psychology50 added the enhancement New feature or request label Oct 14, 2024
@psychology50 psychology50 self-assigned this Oct 14, 2024
@psychology50 psychology50 merged commit a738d10 into dev Oct 14, 2024
1 check passed
@psychology50 psychology50 deleted the feat/PW-578-get-my-chat-room-list branch October 14, 2024 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant