이동국님이 번역해놓은 iBATIS SQL Maps 개발자 가이드를 보고있었습니다.
INSERT를 한번에 많이 해야하는 상황이 발생해서 더 빠르게 하는 방법이 있나? 라는 생각이 들어서 가이드를 보고 있었습니다. 배치라는게 있었는데요.

배치(Batches)
만약 당신이 수행할 많은 수의 쿼리아닌 statement(insert/update/delete)를 가진다면 당신은 추가적인 최적화를 위해서 네트워크 트래픽을 줄이고 JDBC드라이버를 허락하는 배치 같은 작업을 수행하길 원할지도 모른다. 배치를 사용하는 것은 SQL Map API를 사용하면 간단하다. 배치의 경계를 지정하기 위해서 두가지 간단한 메소드를 제공한다.
sqlMap.startBatch();
//…execute statements in between
sqlMap.executeBatch();
executeBatch()를 호출함으로써 모든 배치 statement는 JDBC드라이버를 통해 수행될것이다.

라는 글이 있더군요. 최적화를 시켜주는 것 같은데요. 그럼 더 빠른건가? 라는 생각에 삽질을 해봤습니다.

[code]
@Test
 public void testYesBatch()
 {
  StopWatch stopWatch = new StopWatch();

  stopWatch.start();
  try
  {
   sqlMapClient.startBatch();
   for(int i = 0 ; i < 100000 ; i++)
   {
    Testtemp testtemp = new Testtemp();
    testtemp.setId(i);
    testtemp.setName("성종천");
    sqlMapClient.insert("insertTesttemp", testtemp);
   }
   sqlMapClient.executeBatch();
  }
  catch(SQLException e)
  {
   e.printStackTrace();
  }
  stopWatch.stop();
  System.out.println("배치사용 걸린시간: " + stopWatch.getTotalTimeMillis());
 }
[/code]
Batch를 사용해서 시간을 측정해봤습니다. 100000건을 insert시켜봤는데, 198485가 나왔습니다.

Batch를 안쓰고 해봤습니다.
[code]
@Test
 public void testNoBatch()
 {
  StopWatch stopWatch = new StopWatch();
  stopWatch.start();
  for(int i = 0 ; i < 100000 ; i++)
  {
   Testtemp testtemp = new Testtemp();
   testtemp.setId(i);
   testtemp.setName("성종천");
   sqlMapClientTemplete.insert("insertTesttemp", testtemp);
  }
  stopWatch.stop();
  System.out.println("배치미사용 걸린시간: " + stopWatch.getTotalTimeMillis());
 }
[/code]
100000건 insert하는데 202359나왔습니다.
뭐지 이 차이없다는 것 같은 느낌은.....-_-;

1000건 했을 때는 Batch사용시 20015, 미사용시 20032.......

속도랑은 상관없이 최적화 하는건가-_-; 암튼 뭐 그렇습니다.

 
Posted by 머드초보
,