source 는 Github 에 있습니다.
insert into test (column01, column02) values ('test01', 'test02');
insert into test (column01, column02) values ('test03', 'test04');
insert into test (column01, column02) values ('test05', 'test06');
[hibernate.jdbc.batch_size]
Controls the maximum number of statements Hibernate will batch together before asking the driver to execute the batch. Zero or a negative number disables this feature.
spring:
jpa:
properties:
hibernate:
jdbc:
batch_size: 1000
# before apply option
update test set column01 = 'test3' where regdate = '20210510';
update test2 set column02 = 'test5' where regdate = '20210511';
update test set column01 = 'test4' where regdate = '20210512';
update test2 set column02 = 'test6' where regdate = '20210513';
# after apply option
update test set column01 = 'test3' where regdate = '20210510';
update test set column01 = 'test4' where regdate = '20210512';
update test2 set column02 = 'test5' where regdate = '20210511';
update test2 set column02 = 'test6' where regdate = '20210513';
spring:
jpa:
properties:
hibernate:
jdbc:
order_inserts: true
order_updates: true
# 옵션 켜기 전
insert into test (column01, column02) values ('test01', 'test02');
insert into test (column01, column02) values ('test03', 'test04');
insert into test (column01, column02) values ('test05', 'test06');
# 옵션 켠 후
insert into test (column01, column02)
values ('test01', 'test02'),
('test03', 'test04'),
('test05', 'test06')
// 옵션 켰을 때
[ Test worker] c.example.jpa.api.StudentApiController : elapsed time : 7761
// 옵션 키지 않았을 때
[ Test worker] c.example.jpa.api.StudentApiController : elapsed time : 8974
spring:
jpa:
properties:
hibernate:
jdbc:
batch_size: 1000
order_inserts: true
order_updates: true
@RunWith(SpringRunner.class)
@SpringBootTest(classes= JpaExampleApplication.class)
@AutoConfigureMockMvc
public class StudentApiControllerTest {
@Autowired
private StudentService studentService;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup( new StudentApiController(studentService))
.build();
}
@Test
public void jpaBulkInsertTest() throws Exception {
this.mockMvc.perform(post("/api/training1/students"))
.andExpect(status().isOk());
}
}
@RestController
@RequiredArgsConstructor
public class StudentApiController {
private final StudentService studentService;
/**
* bulk insert test
*/
@PostMapping("/api/training1/students")
public void saveStudents() {
long start = System.currentTimeMillis();
studentService.saveStudents();
log.info("elapsed time : {}", System.currentTimeMillis() - start);
}
}
@Service
@RequiredArgsConstructor
public class StudentService {
private final StudentRepository studentRepository;
public void saveStudents() {
List<Student> list = new ArrayList<Student>() {
{
for (int i = 0 ; i < 200000 ; i++) {
add(Student.builder().name("test" + i)
.age(20)
.address("test" + i)
.etc("test" + i)
.build());
}
}
};
studentRepository.saveAll(list);
}
}
@Entity
@Getter
@NoArgsConstructor
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "student_id")
@Setter
private Long id;
@Column
private String name;
@Column
private int age;
@Column
private String address;
@Column
private String etc;
@Builder
public Student(Long id, String name, int age, String address, String etc) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.etc = etc;
}
}