스프링데이터 + JPA/API 개발

10. 컬렉션 조회 최적화

sdafdq 2023. 11. 14. 12:12

지금까지는 컬렉션 조회해오지는 않았음.

그런 것들의 경우 그냥 join fetch로 해결 가능

 

컬렉션 조회는 일대다 조회가 됨.

뭐 테이블도 

대충 이런 느낌임.

 

 

일단 단계적으로 할거임.

마찬가지로 엔티티 직접 노출 먼저 할거임.

 

@GetMapping("/api/v1/orders")
public List<Order> ordersV1(){
    List<Order> result = orderRepository.findAllString(new OrderSearch());
    for (Order order : result) {
        order.getMember().getName();
        order.getDelivery().getAddress();
        List<OrderItem> orderItems = order.getOrderItems();
        for (OrderItem orderItem : orderItems) {
            orderItem.getItem().getName();
        }
    }
    return result;
}

다 LAZY이기 때문에, 저렇게 한번 씩 만져줘야 로딩함.

그리고

@Bean
Hibernate5JakartaModule hibernate5Module(){
    Hibernate5JakartaModule hibernate5JakartaModule = new Hibernate5JakartaModule();
    return hibernate5JakartaModule;
}

현재 이거 설정해 둬서 기본이 프록시인건 가져오지 않음. 한번씩 만져서 제대로 로딩 된것만 가져옴.

 

값은 정상적으로 가져왔지만,

당연하게도

    select
        o1_0.order_id,
        o1_0.delivery_id,
        o1_0.member_id,
        o1_0.order_date,
        o1_0.status 
    from
        orders o1_0 
    join
        member m1_0 
            on m1_0.member_id=o1_0.member_id 
    fetch
        first ? rows only
2023-11-14T11:55:01.943+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [INTEGER] - [1000]
2023-11-14T11:55:01.944+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501944 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 join member m1_0 on m1_0.member_id=o1_0.member_id fetch first ? rows only
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 join member m1_0 on m1_0.member_id=o1_0.member_id fetch first 1000 rows only;
2023-11-14T11:55:01.964+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        m1_0.member_id,
        m1_0.city,
        m1_0.street,
        m1_0.zipcode,
        m1_0.name 
    from
        member m1_0 
    where
        m1_0.member_id=?
2023-11-14T11:55:01.965+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [1]
2023-11-14T11:55:01.965+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501965 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=?
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=1;
2023-11-14T11:55:01.970+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        d1_0.delivery_id,
        d1_0.city,
        d1_0.street,
        d1_0.zipcode,
        d1_0.status 
    from
        delivery d1_0 
    where
        d1_0.delivery_id=?
2023-11-14T11:55:01.971+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [1]
2023-11-14T11:55:01.972+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501972 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=?
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=1;
2023-11-14T11:55:01.974+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        o1_0.order_id,
        o1_0.delivery_id,
        o1_0.member_id,
        o1_0.order_date,
        o1_0.status 
    from
        orders o1_0 
    where
        o1_0.delivery_id=?
2023-11-14T11:55:01.975+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [1]
2023-11-14T11:55:01.975+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501975 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=?
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=1;
2023-11-14T11:55:01.980+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        o1_0.order_id,
        o1_0.order_item_id,
        o1_0.count,
        o1_0.item_id,
        o1_0.order_price 
    from
        order_item o1_0 
    where
        o1_0.order_id=?
2023-11-14T11:55:01.981+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [1]
2023-11-14T11:55:01.981+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501981 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=?
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=1;
2023-11-14T11:55:01.984+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        i1_0.item_id,
        i1_0.dtype,
        i1_0.name,
        i1_0.price,
        i1_0.stock_quantity,
        i1_0.artist,
        i1_0.etc,
        i1_0.author,
        i1_0.isbn,
        i1_0.actor,
        i1_0.director 
    from
        item i1_0 
    where
        i1_0.item_id=?
2023-11-14T11:55:01.984+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [1]
2023-11-14T11:55:01.985+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501985 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=?
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=1;
2023-11-14T11:55:01.985+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        i1_0.item_id,
        i1_0.dtype,
        i1_0.name,
        i1_0.price,
        i1_0.stock_quantity,
        i1_0.artist,
        i1_0.etc,
        i1_0.author,
        i1_0.isbn,
        i1_0.actor,
        i1_0.director 
    from
        item i1_0 
    where
        i1_0.item_id=?
2023-11-14T11:55:01.986+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [2]
2023-11-14T11:55:01.986+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501986 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=?
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=2;
2023-11-14T11:55:01.987+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        m1_0.member_id,
        m1_0.city,
        m1_0.street,
        m1_0.zipcode,
        m1_0.name 
    from
        member m1_0 
    where
        m1_0.member_id=?
2023-11-14T11:55:01.987+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [2]
2023-11-14T11:55:01.988+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501988 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=?
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=2;
2023-11-14T11:55:01.988+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        d1_0.delivery_id,
        d1_0.city,
        d1_0.street,
        d1_0.zipcode,
        d1_0.status 
    from
        delivery d1_0 
    where
        d1_0.delivery_id=?
2023-11-14T11:55:01.988+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [2]
2023-11-14T11:55:01.989+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501989 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=?
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=2;
2023-11-14T11:55:01.990+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        o1_0.order_id,
        o1_0.delivery_id,
        o1_0.member_id,
        o1_0.order_date,
        o1_0.status 
    from
        orders o1_0 
    where
        o1_0.delivery_id=?
2023-11-14T11:55:01.991+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [2]
2023-11-14T11:55:01.991+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501991 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=?
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=2;
2023-11-14T11:55:01.992+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        o1_0.order_id,
        o1_0.order_item_id,
        o1_0.count,
        o1_0.item_id,
        o1_0.order_price 
    from
        order_item o1_0 
    where
        o1_0.order_id=?
2023-11-14T11:55:01.993+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [2]
2023-11-14T11:55:01.993+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501993 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=?
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=2;
2023-11-14T11:55:01.995+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        i1_0.item_id,
        i1_0.dtype,
        i1_0.name,
        i1_0.price,
        i1_0.stock_quantity,
        i1_0.artist,
        i1_0.etc,
        i1_0.author,
        i1_0.isbn,
        i1_0.actor,
        i1_0.director 
    from
        item i1_0 
    where
        i1_0.item_id=?
2023-11-14T11:55:01.996+09:00 TRACE 12508 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [BIGINT] - [3]
2023-11-14T11:55:01.996+09:00  INFO 12508 --- [nio-8080-exec-2] p6spy                                    : #1699930501996 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/./jpashop
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=?
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=3;
2023-11-14T11:55:01.997+09:00 DEBUG 12508 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        i1_0.item_id,
        i1_0.dtype,
        i1_0.name,
        i1_0.price,
        i1_0.stock_quantity,
        i1_0.artist,
        i1_0.etc,
        i1_0.author,
        i1_0.isbn,
        i1_0.actor,
        i1_0.director 
    from
        item i1_0 
    where
        i1_0.item_id=?

쿼리는 이렇게 나감.

 

 

문제점이 많음.

일단 엔티티도 노출되어있고, 쿼리도 많이나감.

 

우선 엔티티부터 Dto로 바꿀거임.

'스프링데이터 + JPA > API 개발' 카테고리의 다른 글

12. 컬렉션 -> 엔티티 join fetch 최적화  (0) 2023.11.15
11. 컬렉션 엔티티를 Dto로  (0) 2023.11.14
9. 바로 Dto로 받기  (0) 2023.11.14
8. fetch join 최적화  (0) 2023.11.13
7. 엔티티를 Dto로  (0) 2023.11.13