Developer/Java, Kotlin
자바에서 DB Array Processing 하기
데브포유
2011. 3. 28. 14:14
반응형
오라클 pro*c에서 부득이하게 DBMS Call를 통해 클라이언트로 데이터를 fetch할때 또는 반대로 insert, update 할때에
Array Processing를 하게되면 DBMS Call 횟수를 줄여 성능을 향상 시킬 수 있습니다.
pro*c에서는 host변수를 배열로 만들어 Array Processing를 수행합니다.
pro*c로 하는 Array Processing은 쉽게 찾아 볼 수 있음으로 생략하구요.
자바를 활용한 Array Processing 샘플이 있어 소개해 드려요.
출처> http://www.gurubee.net/pages/viewpage.action?pageId=3901805
public class JavaArrayProcessing{ public static void insertData( Connection con , PreparedStatement st , String param1 , String param2 , String param3 , long param4) throws Exception{ st.setString(1, param1); st.setString(2, param2); st.setString(3, param3); st.setLong(4, param4); st.addBatch(); // Batch처리를 위한 묶음으로 등록 } public static void execute(Connection con, String input_month) throws Exception { long rows = 0; String SQLStmt1 = "SELECT 고객번호, 납입월" + " , 지로, 자동이체, 신용카드, 핸드폰, 인터넷 " + "FROM 월요금납부실적 " + "WHERE 납입월 = ?"; String SQLStmt2 = "INSERT /*+ test3 */ INTO 납입방법별_월요금집계 " + "(고객번호, 납입월, 납입방법코드, 납입금액) " + "VALUES(?, ?, ?, ?)"; con.setAutoCommit(false); PreparedStatement stmt1 = con.prepareStatement(SQLStmt1); PreparedStatement stmt2 = con.prepareStatement(SQLStmt2); stmt1.setFetchSize(1000); //Array Size 지정. 너무 커도 좋지 않음. 저는 보통 500 또는 1000을 사용함 stmt1.setString(1, input_month); ResultSet rs = stmt1.executeQuery(); while(rs.next()){ String 고객번호 = rs.getString(1); String 납입월 = rs.getString(2); long 지로 = rs.getLong(3); long 자동이체 = rs.getLong(4); long 신용카드 = rs.getLong(5); long 핸드폰 = rs.getLong(6); long 인터넷 = rs.getLong(7); if(지로 > 0) insertData (con, stmt2, 고객번호, 납입월, "A", 지로); if(자동이체 > 0) insertData (con, stmt2, 고객번호, 납입월, "B", 자동이체); if(신용카드 > 0) insertData (con, stmt2, 고객번호, 납입월, "C", 신용카드); if(핸드폰 > 0) insertData (con, stmt2, 고객번호, 납입월, "D", 핸드폰); if(인터넷 > 0) insertData (con, stmt2, 고객번호, 납입월, "E", 인터넷); if(++rows%1000 == 0) stmt2.executeBatch(); // 1000건마다 배치 수행 } rs.close(); stmt1.close(); stmt2.executeBatch(); stmt2.close(); con.commit(); con.setAutoCommit(true); } public static void main(String[] args) throws Exception{ long btm = System.currentTimeMillis(); Connection con = getConnection(); execute(con, "200903"); System.out.println("elapsed time : " + (System.currentTimeMillis() - btm)); releaseConnection(con); } |
요약하자면 Select 즉 fetch는 ResultSet.setFetchSize() or PreparedStatement.setFetchSize() 함수를 통해서 구현하구요.
Insert, Update는 PreparedStatement.addBatch() 와 PreparedStatement.executeBatch() 함수를 통해서 구현하고 있군요.
addBatch()는 사용한 적이 있었지만 setFetchSize()는 사용해본적이 없었더군요.
반응형