4._Array_Processing_활용
2012.05.14 06:43
1. Array Processing이란?
- 한번의 SQL 수행으로 다량의 ROW를 동시에 INSERT/UPDATE/DELETE 할 수 있다.
- 네트워크를 통한 데이터베이스 Call 감소 궁극적으로 SQL수행 시간과 CPU 사용량을 줄임
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();
}
public static void execute(Connection con, String input_month)
throws Exception {
long rows = 0;
String SQLStmt1 = "SELECT 고갤번호,납입월"
+ " ,지로,자동이체,신용카드, 핸드폰, 인터넷"
+ " FROM 월요금납부실적"
+ " WHERE 납입월 = ?";
String SQLStmt2 = "INSERT INTO 납입방법별_월요금집계"
+ " (고객번호,납입월,납입방법코드, 납입금액)"
+ " VALUES(?,?,?,?)";
con.setAutoCommit(false);
PreparedStatement stmt1 = con.prepareStatement(SQLStmt1);
PreparedStatement stmt2 = con.prepareStatement(SQLStmt2);
stmt1.setFetchSize(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();
}
rs.close();
stmt1.close();
stmt2.executeBatch();
stmt2.close();
con.commit();
con.setAutoCommit(true);
}
static Connection getConnection() throws Exception {....}
static void releaseConnection(Connection con) throws Exception{ ..... }
public static void main (String[] args) throws Exception{
Connection con = getConnection();
execute(con,"200903");
releaseConnection(
}
}
- insert 건수 : 150,000 건 30,000 * 5건
- 수행 시간 : 1.21초
SELECT 고객번호,납입월,지로,자동이체,신용카드,핸드폰,인터넷
FROM 월요금납부실적 WHERE 납입월 = :1
call count cpu elapsed disk query current rows
------- ------ ------ ---------- ------ ------- --------- -------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.01 0.00 0 71 0 0
Fetch 31 0.00 0.04 0 169 0 30000
------- ------ ------ ---------- ------ ------- --------- -------
total 33 0.01 0.04 0 240 0 30000
Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 54
Rows Row source Operation
----- -----------------------------------------------------------
30000 TABLE ACCESS FULL 월요금납부실적 (cr=169 pr=0 pw=0 time=90083 us)
*************************************************************************
INSERT INTO 납입방법별_월요금집계
(고객번호, 납입월, 납입방법코드, 납입금액)
VALUE(:1, :2, :3, :4)
call count cpu elapsed disk query current rows
------- ------ ------ ---------- ------ ------- --------- -------
Parse 1 0.00 0.00 0 0 0 0
Execute 30 0.18 0.27 0 71 5094 150000
Fetch 0 0.00 0.00 0 169 0 0
------- ------ ------ ---------- ------ ------- --------- -------
total 31 0.18 0.27 2 923 5094 150000
Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 54
- Insert : Execute = 30건, 150,000건 => Insert 매번 5,000건씩 Array Processing 한것임
- Select 역시 Fetch 할때 1000개 단위로 Array Fetch 하도록 조정 하였음 (Fetch가 31인것을 확인할수 있음)
| PL/SQL (Recursive) | JAVA | JAVA (Array 처리) | One-SQL |
Parse Call | 5 | 150,000 | 1 | 1 |
Execute Call | 150,000 | 150,000 | 30 | 1 |
총 소요시간 | 7.26초 | 126.82초 | 1.21초 | 0.9초 |
- Array Processing 효과를 그대화 하려면 연속된 일련의 처리과정을 모두 Array 단위로 진행해야 한다.
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
66 | Front Page | 운영자 | 2012.02.21 | 131203 |
65 | 6._문장수준_읽기_일관성 | 정찬호 | 2012.03.12 | 57274 |
64 | 3._SQL트레이스 | sapius | 2012.04.04 | 26852 |
63 | 5._Undo | dasini | 2012.03.11 | 25074 |
62 | 2._SQL_처리과정 | dasini | 2012.04.06 | 21688 |
61 | 6._RAC_캐시_퓨전 | 남송휘 | 2012.05.21 | 17530 |
60 | 4._커서_공유 | 남송휘 | 2012.04.27 | 16112 |
59 | 5._오라클_Lock | 시와처 | 2012.03.26 | 12579 |
58 | 8._블록_클린아웃 | 시와처 | 2012.03.19 | 12015 |
57 | 4._동시성_구현_사례 [1] | dasini | 2012.03.27 | 11697 |
56 | 8._Statspack_AWR | 시와처 | 2012.04.01 | 11488 |
55 | 2._DB_버퍼_캐시 | 시와처 | 2012.03.04 | 10432 |
54 | 10._대기_이벤트 | 박영창 | 2012.03.19 | 10373 |
53 | 9._Snapshot_too_old | 박영창 | 2012.03.19 | 9734 |
52 | 1._기본_아키텍처 | AskZZang | 2012.03.02 | 8350 |
51 | 7._세션_커서_캐싱 | 박영창 | 2012.04.22 | 7793 |
50 | 5._Direct_Path_IO | 남송휘 | 2012.05.21 | 7553 |
49 | 3._버퍼_Lock | 박영창 | 2012.02.24 | 7260 |
48 | 7._PLSQL_함수의_특징과_성능_부하 | 남송휘 | 2012.05.15 | 6642 |
47 | 10._V$SQL | 정찬호 | 2012.04.09 | 6544 |