소개
게시글
질문&답변
2021.07.19
default_batch_fetch_size의 원리가 궁금합니다!
앗 그렇군요! 지금 살펴보니 쿼리 -> 10개 출력 -> 쿼리 -> 10개 출력... 순서로 되어있는 게 맞습니다! Hibernate: select orderitems0_.order_id as order_id5_5_1_, orderitems0_.order_iem_id as order_ie1_5_1_, orderitems0_.order_iem_id as order_ie1_5_0_, orderitems0_.count as count2_5_0_, orderitems0_.item_id as item_id4_5_0_, orderitems0_.order_id as order_id5_5_0_, orderitems0_.order_price as order_pr3_5_0_ from order_item orderitems0_ where orderitems0_.order_id in ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) orderItem count : 1 orderItem count : 2 orderItem count : 3 orderItem count : 4 orderItem count : 5 orderItem count : 6 orderItem count : 7 orderItem count : 8 orderItem count : 9 orderItem count : 10 Hibernate: select orderitems0_.order_id as order_id5_5_1_, orderitems0_.order_iem_id as order_ie1_5_1_, orderitems0_.order_iem_id as order_ie1_5_0_, orderitems0_.count as count2_5_0_, orderitems0_.item_id as item_id4_5_0_, orderitems0_.order_id as order_id5_5_0_, orderitems0_.order_price as order_pr3_5_0_ from order_item orderitems0_ where orderitems0_.order_id in ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) orderItem count : 11 orderItem count : 12 orderItem count : 13 orderItem count : 14 orderItem count : 15 orderItem count : 16 orderItem count : 17 orderItem count : 18 orderItem count : 19 orderItem count : 20 ... 그런데, 아직도 이해가 어려운 것은 미리 default_batch_fetch_size 만큼의 데이터를 가져오기 위해서는 어떤 데이터가 미래에 사용하는 데이터인지를 알아야 하는 것 아닌가요..?! 그렇지 않다면 단순히 id기준으로 뒤의 default_batch_fetch_size개 데이터를 가져오는 것인가요?
- 0
- 4
- 529
질문&답변
2021.07.17
default_batch_fetch_size의 원리가 궁금합니다!
말씀해주신대로 테스트를 해봤습니다! InitDb에서 아래의 코드로 Order에 OrderItem을 한개씩 등록하여 총 100개의 주문을 생성했습니다 for (int i = 1; i 100; i++) { OrderItem orderItem1 = OrderItem.createOrderItem(book1, 10000, i); Order order = Order.createOrder(member, delivery, orderItem1); em.persist(order);} default_batch_fetch_size=10 으로 지정했고, OrderApiController의 코드를 아래와 같이 for문으로 풀어쓰고 System.out을 통해 어떤 식으로 쿼리가 발생하는지 확인했습니다 for (OrderItem orderItem : order.getOrderItems()) { OrderItemDto orderItemDto = new OrderItemDto(orderItem); System.out.println("orderItem count : " + orderItem.getCount()); list.add(orderItemDto);} 이때, count를 1~100의 숫자로 순차적으로 저장했기 때문에, getCount()를 통해 몇번째 orderItem이 출력되고 있는지 확인했습니다. 아래의 코드를 통해서 각각의 order를 순회하면서 위 코드를 호출하게 되고, orderItem을 하나씩 지연로딩 시킵니다 for (Order order : orders) { result.add(new OrderDto(order));} 결과는 아래와 같습니다 orderItem count : 1 orderItem count : 2 orderItem count : 3 orderItem count : 4 orderItem count : 5 orderItem count : 6 orderItem count : 7 orderItem count : 8 orderItem count : 9 orderItem count : 10 Hibernate: select orderitems0_.order_id as order_id5_5_1_, orderitems0_.order_iem_id as order_ie1_5_1_, orderitems0_.order_iem_id as order_ie1_5_0_, orderitems0_.count as count2_5_0_, orderitems0_.item_id as item_id4_5_0_, orderitems0_.order_id as order_id5_5_0_, orderitems0_.order_price as order_pr3_5_0_ from order_item orderitems0_ where orderitems0_.order_id in ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) orderItem count : 11 orderItem count : 12 orderItem count : 13 orderItem count : 14 orderItem count : 15 orderItem count : 16 orderItem count : 17 orderItem count : 18 orderItem count : 19 orderItem count : 20 Hibernate: select orderitems0_.order_id as order_id5_5_1_, orderitems0_.order_iem_id as order_ie1_5_1_, orderitems0_.order_iem_id as order_ie1_5_0_, orderitems0_.count as count2_5_0_, orderitems0_.item_id as item_id4_5_0_, orderitems0_.order_id as order_id5_5_0_, orderitems0_.order_price as order_pr3_5_0_ from order_item orderitems0_ where orderitems0_.order_id in ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ... 테스트 결과, 지연로딩이 발생해야 하는 객체를 버퍼처럼 10개씩 모아놨다가, default_batch_fetch_size로 지정한 개수가 다 차게 되면 쿼리가 발생하는 것을 확인했습니다! 감사합니다 :)
- 0
- 4
- 529