네이버 클라우드 캠프

인터페이스 프로젝션

99duuk 2024. 6. 24. 19:28

@EntityGreaph 사용해 

Reply 엔티티와 연관된 Board 및 Member 엔티티를 함께 조회

Hibernate: 
    select
        b1_0.id,
        b1_0.category_name,
        b1_0.content,
        b1_0.is_deleted,
        b1_0.reg_date,
        b1_0.title,
        b1_0.views,
        b1_0.writer_id 
    from
        board b1_0 
    where
        b1_0.id=?
Hibernate: 
    select
        m1_0.id,
        m1_0.email,
        m1_0.grade,
        m1_0.is_active,
        m1_0.nickname,
        m1_0.password,
        m1_0.point 
    from
        member m1_0 
    where
        m1_0.id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        b2_0.id,
        b2_0.category_name,
        b2_0.content,
        b2_0.is_deleted,
        b2_0.reg_date,
        b2_0.title,
        b2_0.views,
        b2_0.writer_id,
        r1_0.content,
        r1_0.parent_id,
        r1_0.reg_date,
        r1_0.writer_id,
        w2_0.id,
        w2_0.email,
        w2_0.grade,
        w2_0.is_active,
        w2_0.nickname,
        w2_0.password,
        w2_0.point 
    from
        reply r1_0 
    join
        board b2_0 
            on b2_0.id=r1_0.board_id 
    join
        member w2_0 
            on w2_0.id=r1_0.writer_id 
    where
        r1_0.board_id=? 
        and r1_0.parent_id is null 
    limit
        ?, ?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        b1_0.id,
        b1_0.category_name,
        b1_0.content,
        b1_0.is_deleted,
        b1_0.reg_date,
        b1_0.title,
        b1_0.views,
        b1_0.writer_id,
        r1_0.content,
        r1_0.parent_id,
        r1_0.reg_date,
        r1_0.writer_id,
        w2_0.id,
        w2_0.email,
        w2_0.grade,
        w2_0.is_active,
        w2_0.nickname,
        w2_0.password,
        w2_0.point 
    from
        reply r1_0 
    join
        board b1_0 
            on b1_0.id=r1_0.board_id 
    join
        member w2_0 
            on w2_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        b1_0.id,
        b1_0.category_name,
        b1_0.content,
        b1_0.is_deleted,
        b1_0.reg_date,
        b1_0.title,
        b1_0.views,
        b1_0.writer_id,
        r1_0.content,
        r1_0.parent_id,
        r1_0.reg_date,
        r1_0.writer_id,
        w2_0.id,
        w2_0.email,
        w2_0.grade,
        w2_0.is_active,
        w2_0.nickname,
        w2_0.password,
        w2_0.point 
    from
        reply r1_0 
    join
        board b1_0 
            on b1_0.id=r1_0.board_id 
    join
        member w2_0 
            on w2_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        b1_0.id,
        b1_0.category_name,
        b1_0.content,
        b1_0.is_deleted,
        b1_0.reg_date,
        b1_0.title,
        b1_0.views,
        b1_0.writer_id,
        r1_0.content,
        r1_0.parent_id,
        r1_0.reg_date,
        r1_0.writer_id,
        w2_0.id,
        w2_0.email,
        w2_0.grade,
        w2_0.is_active,
        w2_0.nickname,
        w2_0.password,
        w2_0.point 
    from
        reply r1_0 
    join
        board b1_0 
            on b1_0.id=r1_0.board_id 
    join
        member w2_0 
            on w2_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        b1_0.id,
        b1_0.category_name,
        b1_0.content,
        b1_0.is_deleted,
        b1_0.reg_date,
        b1_0.title,
        b1_0.views,
        b1_0.writer_id,
        r1_0.content,
        r1_0.parent_id,
        r1_0.reg_date,
        r1_0.writer_id,
        w2_0.id,
        w2_0.email,
        w2_0.grade,
        w2_0.is_active,
        w2_0.nickname,
        w2_0.password,
        w2_0.point 
    from
        reply r1_0 
    join
        board b1_0 
            on b1_0.id=r1_0.board_id 
    join
        member w2_0 
            on w2_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?


reply, board, member 테이블을 조인하여 reply 엔티티와 관련된 모든 필드를 조회



 

 

 

인터페이스 프로젝션
 IReply 인터페이스에 정의된 필드만 조회

Hibernate: 
    select
        b1_0.id,
        b1_0.category_name,
        b1_0.content,
        b1_0.is_deleted,
        b1_0.reg_date,
        b1_0.title,
        b1_0.views,
        b1_0.writer_id 
    from
        board b1_0 
    where
        b1_0.id=?
Hibernate: 
    select
        m1_0.id,
        m1_0.email,
        m1_0.grade,
        m1_0.is_active,
        m1_0.nickname,
        m1_0.password,
        m1_0.point 
    from
        member m1_0 
    where
        m1_0.id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        r1_0.content,
        w1_0.id,
        w1_0.email,
        w1_0.grade,
        w1_0.is_active,
        w1_0.nickname,
        w1_0.password,
        w1_0.point 
    from
        reply r1_0 
    left join
        member w1_0 
            on w1_0.id=r1_0.writer_id 
    where
        r1_0.board_id=? 
        and r1_0.parent_id is null 
    limit
        ?, ?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        r1_0.content,
        w1_0.id,
        w1_0.email,
        w1_0.grade,
        w1_0.is_active,
        w1_0.nickname,
        w1_0.password,
        w1_0.point 
    from
        reply r1_0 
    left join
        member w1_0 
            on w1_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        r1_0.content,
        w1_0.id,
        w1_0.email,
        w1_0.grade,
        w1_0.is_active,
        w1_0.nickname,
        w1_0.password,
        w1_0.point 
    from
        reply r1_0 
    left join
        member w1_0 
            on w1_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        r1_0.content,
        w1_0.id,
        w1_0.email,
        w1_0.grade,
        w1_0.is_active,
        w1_0.nickname,
        w1_0.password,
        w1_0.point 
    from
        reply r1_0 
    left join
        member w1_0 
            on w1_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?
Hibernate: 
    select
        r1_0.id,
        r1_0.board_id,
        r1_0.content,
        w1_0.id,
        w1_0.email,
        w1_0.grade,
        w1_0.is_active,
        w1_0.nickname,
        w1_0.password,
        w1_0.point 
    from
        reply r1_0 
    left join
        member w1_0 
            on w1_0.id=r1_0.writer_id 
    where
        r1_0.parent_id=?

reply와 member 테이블만 조인하여 reply 엔티티의 일부 필드와 writer 필드만 조회

필요한 데이터만 선택적으로 조회하여 성능을 최적화

 

 

 

 

주요 차이점

| 조회 필드의 범위

- 기존 쿼리: 모든 연관된 엔티티의 모든 필드를 조회한다.

- 인터페이스 프로젝션 쿼리: 인터페이스에 정의된 필드만 선택적으로 조회한다. 이는 데이터 전송량을 줄이고 성능을 최적화할 수 있다.

 

| 조인 전략:

- 기존 쿼리: JOIN 구문을 사용하여 여러 테이블을 조인한다.

- 인터페이스 프로젝션 쿼리: 필요한 연관 엔티티만 선택적으로 JOIN한다. 예를 들어, reply와 member 테이블만 조인하고 board 테이블은 조인하지 않는다.

 

| 쿼리 결과의 크기:

- 기존 쿼리: 많은 양의 데이터를 조회하여 전송한다.

- 인터페이스 프로젝션 쿼리: 필요한 필드만 조회하여 전송량을 줄인다.

 

 

==> 불필요한 데이터 전송 감소, 쿼리 최적화, 메모리 사용량 감소, 네트워크 대역폭 사용 감소 라는데 정확한진 모르겠다.

'네이버 클라우드 캠프' 카테고리의 다른 글

Projection을 사용한 명명 메서드: 성능 비교와 느낀 점  (0) 2024.07.29
Spring data JPA vs ElasticSearch  (0) 2024.07.24
JWT & Redis  (0) 2024.06.19
VPC Subnet ACL ACG  (0) 2024.06.14
로드 밸런서 구성  (0) 2024.06.13