개발

JAVA - PostgreSQL Multi Value Insert

ZHVVVVVVV 2025. 5. 28. 11:26
728x90
반응형

PostgreSQL 에서 일반적인

INSERT INTO 테이블명 VALUES(...) 방식이 아닌

INSERT INTO 테이블명 VALUES(...),(...),(...)...

이렇게 VALUES 값들을 늘여 놓을 수 있는 기능이 있는 것을 알게 되었습니다.

https://www.timescale.com/learn/testing-postgres-ingest-insert-vs-batch-insert-vs-copy

 

Testing Postgres Ingest: INSERT vs. Batch INSERT vs. COPY | Timescale

There are many ways to insert data into Postgres. But which is faster? We benchmarked Postgres INSERT vs. batch INSERT vs. COPY to find out.

www.timescale.com

위 링크를 보면, 일반적인 INSERT 방식보다 성능이 더 우수하다는 것을 확인할 수 있는데,
이번 포스팅에서는 Java에서 Multi-Value INSERT를 어떻게 구현할 수 있는지 공유드리겠습니다.

 

보통 JDBC 로 INSERT를 할 때, PreparedStatement 방식에 addBatch, executeBatch 로 데이터를 넣어주는데,

코드 상에서는 다르게 해줄 것은 없고 JDBC 클라이언트 설정에서 reWriteBatchedInserts=true 로 값을 설정하면 Multi Value Insert 형식으로 값이 들어가게 됩니다.

 

[JDBC 클라이언트 설정 방법]

jdbc:postgresql://localhost:5432/mydb?reWriteBatchedInserts=true

 

[설정에 따른 PostgreSQL db 로그]

  • reWriteBatchedInserts 값 설정하지 않은 경우 => 일반적인 INSERT 방식

  • reWriteBatchedInserts 값을 true 로 설정한 경우 => Multi Value Insert 방식

 

이상입니다.
궁금한 점이 있으시면 댓글로 남겨주세요.

728x90
반응형