JPA

[JPA] batch insert

봉주니 2022. 2. 24. 12:01
  • batch insert 는 여러 개의 SQL 을 한 번에 하나의 트랙잭션으로 처리 할 수 있는 기능을 말하며,

정확히는 JDBC-batching 기능이다. 

 

[Hibernate 공식문서]

 

Hibernate ORM 5.4.33.Final User Guide

Fetching, essentially, is the process of grabbing data from the database and making it available to the application. Tuning how an application does fetching is one of the biggest factors in determining how an application will perform. Fetching too much dat

docs.jboss.org

 

[설정하기]

1. applicaion.yml에 설정

spring:
  jpa:
    properties:
      hibernate:
        jdbc.batch_size: 100
        order_inserts: true
        order_updates: true
  datasource:
    hikari:
      data-source-properties:
        rewriteBatchedStatements: true
  • batch_size : 한 번에 처리할 statement 갯수 설정
  • rewriteBatchedStatements: true 설정    Default : false

MySQL 에서는 위 설정이 있어야 실제로 동작한다. (공식문서 참조)

 

2. hibernate.order_updates, hibernate.order_inserts

 

<설정전>

INSERT INTO TABLE_A (Column1) values ('value1');
INSERT INTO TABLE_B (Column1) values ('value1');
INSERT INTO TABLE_A (Column1) values ('value2');
INSERT INTO TABLE_B (Column1) values ('value2');

<설정후>

INSERT INTO TABLE_A (Column1) values ('value1'),('value2');
INSERT INTO TABLE_B (Column1) values ('value1'),('value2');

 

 

[GenerationType.IDENTITY]

Entity Key 생성을 GenerationType.IDENTITY 로 하면 batch 옵션이 동작안합니다.IDENTITY 인 경우, db에 insert를 실행할 때 알 수 있는 값이기 때문에, 트랙잭션 이후에 확인 가능한 값이기에 batch insert를 사용할 수 없다.

반응형