[java] DB에 다량건 데이터 INSERT 시 excuteBatch() 사용

program/java 2012. 10. 5. 11:15
반응형

Q. 약 15만 라인의 데이터를 txt 파일을 읽어와서
DB에 INSERT 몇개 단위로 배치를 돌릴경우 가장 효율적인가?


Query를 excute 할 때, excuteUpdate()와 excuteBatch()의 성능을 테스트한다.




※ 소스코드


long start = System.nanoTime();
while( ( s = reader.readLine() ) != null )
{
	totCnt++;
	String no = s.substring( 0, 6 );
	int index1 = s.indexOf( '|', 11 );
	int index2 = s.indexOf( '|', index1 + 1 );
	String address = s.substring( index1 + 1, index2 );

	stmt.setInt( 1, totCnt );
	stmt.setString( 2, no );
	stmt.setString( 3, address );

	// case 1
	stmt.executeUpdate();

	// case 2
	stmt.addBatch();
	cnt++;

	if( cnt == 100 )
	{
		cnt = 0;
		stmt.executeBatch();
	}
}




1. 매번 excuteUpdate() 실행

  - 4분 이상의 작업에 인내심이 바닥남.... 중지함.


2. addbatch(), excuteBatch() 를 이용 100건당 실행

  - 39459776065(ns) => 3.9초


3. addbatch(), excuteBatch() 를 이용 500건당 실행

  - 11512822395(ns) => 1.1초


4. addbatch(), excuteBatch() 를 이용 1000건당 실행

  - 6683119024(ns) => 0.66초


5. addbatch(), excuteBatch() 를 이용 10000건당 실행 (BEST 乃)

  - 1442062299(ns) => 0.144초

  - 1423391208(ns) => 0.142초

  - 1475451583(ns) => 0.147초


6. addbatch(), excuteBatch() 를 이용 20000건당 실행 (Rank 3)

  - 1690612350(ns) => 0.16초

  - 1597981482(ns) => 0.15초


7. addbatch(), excuteBatch() 를 이용 30000건당 실행 (Rank 4)

  - 1608968179(ns) => 0.16초

  - 1619791589(ns) => 0.16초


8. addbatch(), excuteBatch() 를 이용 50000건당 실행 (Rank 2)

  - 1488951363(ns) => 0.148초

  - 1466872514(ns) => 0.146초





※ 결론.


1. 다량건 처리는 excuteUpdate 보다 excuteBatch 를 이용해야함!


2. batch 처리시 많은량을 batch로 넣는다해서 (무조건) 빨리 되는건 아님.


3. batch 처리할때에도 적당한 양 만큼 batch 로 처리하는것이 올바름.


4. 테스트 횟수와 각 장비들의 상태가 같지 않았기 때문에, 정확한 결론은 아님(참고할순 있지 않나..?)

반응형