스프링 배치를 활용하여 오픈 API를 업데이트 하는 작업중이다. 대량의 데이터를 delete해야 하는 상황에서 두가지 옵션이 눈에 띄었다. 이 글을 통해 둘을 비교해보고자 한다.
내부 코드를 확인해보면 아래와 같다.
@Transactional
public void deleteAll() {
Iterator var2 = this.findAll().iterator();
while(var2.hasNext()) {
T element = (Object)var2.next();
this.delete(element);
}
}
우선 모든 값을 찾은 후 -> 찾은 값을 일일이 하나씩 delete 한다. 즉, row의 개수만큼 delete()가 일어나는 것이다.
@Transactional
public void deleteAllInBatch() {
Query query = this.entityManager.createQuery(this.getDeleteAllQueryString());
this.applyQueryHints(query);
query.executeUpdate();
}
private String getDeleteAllQueryString() {
return QueryUtils.getQueryString("delete from %s x", this.entityInformation.getEntityName());
}
public static String getQueryString(String template, String entityName) {
Assert.hasText(entityName, "Entity name must not be null or empty");
return String.format(template, entityName);
}
deleteAllInBatch()의 코드를 타고 들어가보면 엔티티 전체를 받아 해당 테이블 전체를 한번에 delete하게끔 진행한다.
어느 메소드가 좋다 우열을 가릴 순 없지만 각각의 특징을 이해하고 개인의 상황에 맞춰 사용할줄은 알아야 한다고 생각한다. 나의 경우, 매일 자정 스프링 배치로 다량의 데이터를 처리한다. 각 데이터는 22개의 컬럼값을 지니게 되는데, 데이터가 늘어날 수록 부하가 생길것이라 예상하여 deleteAllInBatch()를 활용하였다.
전체적인 코드는 아직 손을 봐야할 곳이 많다. 하지만 이런 작지만 중요한 조각들을 하나씩 인지하고 챙겨가기 위해 시작한 프로젝트이니, 차근차근 정리하며 탄탄히 진행할 예정이다.