인프런 커뮤니티 질문&답변

gogo님의 프로필 이미지

작성한 질문수

실전! 스프링 부트와 JPA 활용2 - API 개발과 성능 최적화

간단한 주문 조회 V1: 엔티티를 직접 노출

쿼리 발생 수에 대해서 궁금합니다~~

작성

·

128

·

수정됨

0

@GetMapping("/api/v1/simple-orders")
public List<Order> ordersV1() { //
    List<Order> allByString = orderRepository.findAllByString(new OrderSearch());   //order를 전부 끌고옴 => 2개
    for (Order order : allByString) {
        order.getMember().getName();        
        order.getDelivery().getAddress();   
    }
    return allByString;
}

제가 생각하는 건

    List<Order> allByString = orderRepository.findAllByString(new OrderSearch());

이 코드로 쿼리 1개가 발생하고 결과 값이 2개니깐
for문으로
order.getMember().getName(); 이 쿼리가 2번 발생
order.getDelivery().getAddress(); 이 쿼리가 2번 발생해서 총 5번 쿼리가 발생해야 된다고 생각이 들었습니다.

하지만 로그를 보니깐 쿼리는 총 6번 발생하고

제가 생각했던 거와는 다르게 member와 delivery에 대한 쿼리가 1번씩만 발생하고

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번 발생이 되더라고요

그리고 order_item도 조회가 되는 걸 확인을 했습니다.
로그 쿼리 수는 총 6개였습니다.

전체 쿼리 로그입니다.

전체 쿼리입니다.

2024-08-15T18:49:21.937+09:00 DEBUG 20740 --- [nio-8080-exec-5] 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

join

member m1_0

on m1_0.member_id=o1_0.member_id

fetch

first ? rows only

2024-08-15T18:49:21.940+09:00 INFO 20740 --- [nio-8080-exec-5] p6spy : #1723715361940 | 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;

2024-08-15T18:49:21.942+09:00 DEBUG 20740 --- [nio-8080-exec-5] 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 in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

2024-08-15T18:49:21.943+09:00 INFO 20740 --- [nio-8080-exec-5] p6spy : #1723715361943 | 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 in (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)

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 in (1,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);

2024-08-15T18:49:21.945+09:00 DEBUG 20740 --- [nio-8080-exec-5] 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 in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

2024-08-15T18:49:21.947+09:00 INFO 20740 --- [nio-8080-exec-5] p6spy : #1723715361947 | 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 in (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)

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 in (1,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);

2024-08-15T18:49:21.948+09:00 DEBUG 20740 --- [nio-8080-exec-5] 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=?

2024-08-15T18:49:21.949+09:00 INFO 20740 --- [nio-8080-exec-5] p6spy : #1723715361949 | 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;

2024-08-15T18:49:21.950+09:00 DEBUG 20740 --- [nio-8080-exec-5] 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=?

2024-08-15T18:49:21.951+09:00 INFO 20740 --- [nio-8080-exec-5] p6spy : #1723715361951 | 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;

2024-08-15T18:49:21.954+09:00 DEBUG 20740 --- [nio-8080-exec-5] org.hibernate.SQL :

select

oi1_0.order_id,

oi1_0.order_item_id,

oi1_0.count,

oi1_0.item_id,

oi1_0.order_price

from

order_item oi1_0

where

oi1_0.order_id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

2024-08-15T18:49:21.956+09:00 INFO 20740 --- [nio-8080-exec-5] p6spy : #1723715361956 | took 0ms | statement | connection 7| url jdbc:h2:tcp://localhost/~/jpashop

select oi1_0.order_id,oi1_0.order_item_id,oi1_0.count,oi1_0.item_id,oi1_0.order_price from order_item oi1_0 where oi1_0.order_id in (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)

select oi1_0.order_id,oi1_0.order_item_id,oi1_0.count,oi1_0.item_id,oi1_0.order_price from order_item oi1_0 where oi1_0.order_id in (1,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);

 


왜 orders o1_0 가 2번 조회가 되고 왜 order_item이 조회가 되는지 이해가 잘 안갑니다.

제가 생각했던 거와는 너무 달라서 질문드립니다ㅠㅠ

답변 1

0

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. gogo님

도움을 드리고 싶지만 질문 내용만으로는 답변을 드리기 어렵습니다.

실제 동작하는 전체 프로젝트를 ZIP파일로 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

 

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

 

추가로 다음 내용도 코멘트 부탁드립니다.

1. 문제 영역을 실행할 수 있는 방법

2. 문제가 어떻게 나타나는지에 대한 상세한 설명 (오류 화면, 오류 로그 포함)

 

링크: 공식 서포터즈

링크: 자주하는 질문

감사합니다.

gogo님의 프로필 이미지

작성한 질문수

질문하기