Bulk Insert

Bulk Insert with MySQL

Batch Insert ์„ฑ๋Šฅ ํ–ฅ์ƒ๊ธฐ 1ํŽธ - With JPA - Yun Blog | ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ

GenerationType.IDENTITY๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Batch Insert๊ฐ€ ์•ˆ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

Spring Data์—์„œ Batch Insert ์ตœ์ ํ™”

Hibernate๊ฐ€ ์ฑ„ํƒํ•œ Transactional Write Behind์™€ ์ถœ๋Œ์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ JDBC ์ˆ˜์ค€์—์„œ Batch Insert๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

The only drawback is that we canโ€™t know the newly assigned value prior to executing the INSERT statement. This restriction is hindering the โ€œtransactional write behindโ€ flushing strategy adopted by Hibernate. For this reason, Hibernates disables the JDBC batch support for entities using the IDENTITY generator.

ํ•ด๊ฒฐ์ฑ…

  1. MySQL์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด TABLE์ด๋‚˜ SEQUENCE ๋ฅผ ์จ์•ผํ•  ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ํ•˜์ง€๋งŒ ๋ณ„๋‹ค๋ฅธ ์กฐ์น˜ ์—†์ด ์‚ฌ์šฉํ•˜๋ฉด Batch Insert๋ฅผ ๋ชป์“ฐ๋Š” IDENTITY ๋ณด๋‹ค๋„ ๋Š๋ฆฌ๋‹ค.

    dev-tips/JPA-GenerationType-๋ณ„-INSERT-์„ฑ๋Šฅ-๋น„๊ต.md at master ยท HomoEfficio/dev-tips

  2. Spring Data JPA๋ฅผ ๋ฒ—์–ด๋‚˜ JDBC๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

    • JdbcTemplate์˜ batchUpdate() ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    • ์‹ค์ œ๋กœ ์„ฑ๋Šฅ์€ ๊ฐ€์žฅ ๋น ๋ฅด๋‹ค๊ณ  ํ•œ๋‹ค. (ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.)

๊ฒฐ๋ก 

  • ์•„์ฃผ ๋งŽ์€ ์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๊บผ๋ฒˆ์— ์ž…๋ ฅํ•  ๋•Œ๋Š” Spring Data JPA๋ฅผ ์ž ์‹œ ๋’ค๋กœํ•˜๊ณ  Spring Data JDBC์˜ batchUpdate()๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ๋„ ์ข‹๋‹ค.

    • Spring Data JDBC๋Š” Spring Data JPA์™€ ํ•จ๊ป˜ ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , @Transactional์„ ํ†ตํ•ด ํŠธ๋žœ์žญ์…˜์ด ๊ด€๋ฆฌ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ˜„์‹ค์ ์œผ๋กœ ๊ฐ€์žฅ ๋‚˜์€ ๋ฐฉ๋ฒ•์ด๋‹ค.

  • Spring Data JPA๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•œ๋‹ค๋ฉด IDENTITY ๋ฐฉ์‹ ๋ง๊ณ  Batch SEQUENCE ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

    • ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ์‹์€ ์• ๋„ˆํ…Œ์ด์…˜ ์ง€์ •์ด ํ•„์š” ์ด์ƒ์œผ๋กœ ๋ณต์žกํ•˜๊ณ ,

    • ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์‹œ๋ถ€ํ„ฐ ์ ์šฉํ•˜๋ฉด ๊ดœ์ฐฎ์ง€๋งŒ, ์ด๋ฏธ ID ์ƒ์„ฑ ๋ฐฉ์‹์ด IDENTITY์ธ ๊ธฐ์กด ํ…Œ์ด๋ธ”์„ SEQUENCE ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋‹ด์ด ์žˆ๊ณ ,

    • batch ํฌ๊ธฐ ์ง€์ • ๊ด€๋ จ ์šด์˜ ์ƒ์˜ ๋‹จ์ ์ด ์žˆ๋‹ค.

Last updated