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

kamser님의 프로필 이미지
kamser

작성한 질문수

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

JPA 트랜잭션과 batch_fetch_size 질문이 있습니다.

해결된 질문

작성

·

592

0

@Transactional
public List<Order> findAllWithMemberDelivery(int offset,int limit) {
	List<Order> resultList = em.createQuery(
			"select o from Order o" +
				" join fetch o.member m" +
				" join fetch o.delivery d", Order.class)
		.setFirstResult(offset)
		.setMaxResults(limit)
		.getResultList();
	return resultList;
}

1. @Transaction이 있을 때 조회쿼리

select
        o1_0.order_id,
        d1_0.delivery_id,
        d1_0.city,
        d1_0.street,
        d1_0.zipcode,
        d1_0.status,
        m1_0.member_id,
        m1_0.city,
        m1_0.street,
        m1_0.zipcode,
        m1_0.name,
        o1_0.order_date,
        o1_0.status 
    from
        orders o1_0 
    join
        member m1_0 
            on m1_0.member_id=o1_0.member_id 
    join
        delivery d1_0 
            on d1_0.delivery_id=o1_0.delivery_id offset ? rows fetch first ? rows only
2023-07-15T01:15:30.105+09:00 TRACE 20476 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [INTEGER] - [0]
2023-07-15T01:15:30.105+09:00 TRACE 20476 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [2] as [INTEGER] - [100]
select
        o1_0.order_id,
        o1_0.oder_item_id,
        o1_0.count,
        o1_0.item_id,
        o1_0.order_price 
    from
        order_item o1_0 
    where
        array_contains(?,o1_0.order_id)
2023-07-15T01:15:30.122+09:00 TRACE 20476 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [ARRAY] - [[1, null, null, null,...]
    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
        array_contains(?,i1_0.item_id)
2023-07-15T01:15:30.139+09:00 TRACE 20476 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [ARRAY] - [[2, 1, null, null, null,...]

    select
        o1_0.order_id,
        o1_0.oder_item_id,
        o1_0.count,
        o1_0.item_id,
        o1_0.order_price 
    from
        order_item o1_0 
    where
        array_contains(?,o1_0.order_id)
2023-07-15T01:15:30.142+09:00 TRACE 20476 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [ARRAY] - [[2, null, ...]

    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
        array_contains(?,i1_0.item_id)
2023-07-15T01:15:30.144+09:00 TRACE 20476 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [ARRAY] - [[4, 3, null, null, null,...]

 

public List<Order> findAllWithMemberDelivery(int offset,int limit) {
	List<Order> resultList = em.createQuery(
			"select o from Order o" +
				" join fetch o.member m" +
				" join fetch o.delivery d", Order.class)
		.setFirstResult(offset)
		.setMaxResults(limit)
		.getResultList();
	return resultList;
}

트랜잭션 없이 실행된 쿼리

    select
        o1_0.order_id,
        d1_0.delivery_id,
        d1_0.city,
        d1_0.street,
        d1_0.zipcode,
        d1_0.status,
        m1_0.member_id,
        m1_0.city,
        m1_0.street,
        m1_0.zipcode,
        m1_0.name,
        o1_0.order_date,
        o1_0.status 
    from
        orders o1_0 
    join
        member m1_0 
            on m1_0.member_id=o1_0.member_id 
    join
        delivery d1_0 
            on d1_0.delivery_id=o1_0.delivery_id offset ? rows fetch first ? rows only
2023-07-15T01:19:19.943+09:00 TRACE 14616 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [INTEGER] - [0]
2023-07-15T01:19:19.943+09:00 TRACE 14616 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [2] as [INTEGER] - [100]

    select
        o1_0.order_id,
        o1_0.oder_item_id,
        o1_0.count,
        o1_0.item_id,
        o1_0.order_price 
    from
        order_item o1_0 
    where
        array_contains(?,o1_0.order_id)
2023-07-15T01:19:19.957+09:00 TRACE 14616 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [ARRAY] - [[1, 2, null, null,...]

    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
        array_contains(?,i1_0.item_id)
2023-07-15T01:19:19.977+09:00 TRACE 14616 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [ARRAY] - [[2, 1, 4, 3, null, null, null, null, null, null]]

 

@트랜잭션이 있을 때에는 쿼리가 5번 실행이 되고

@트랜잭션이 없을 때에는 쿼리가 3번이 실행이 됩니다.

 

왜 이렇게 동작하는지 생각을 해도 이해가 잘 안되네요

답변 1

0

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

안녕하세요. kamser님

다음을 참고해주세요.

https://www.inflearn.com/questions/427216

감사합니다.

kamser님의 프로필 이미지
kamser

작성한 질문수

질문하기