๐ท๐ผ๋ชจ์ฌ๋ด์ ๊ฐ๋ฐ์์ฒ๐ท๐ผ
[ํ๋ธ๋ฆฌ๋] ํ๋ก์์ ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ณธ๋ฌธ
๋ฐ์ํ
์ด๋๋ ํ ์ด๋ธ์ 5000๊ฑด ์ด์์ ๋ฐ์ดํฐ๊ฐ ์ฐ๊ณ๋์ด ๋ค์ด์ ํ๋ก์์ ๋ฅผ ์คํํ๋ฉฐ DB๊ฐ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํ๋ ํ์์ด ์์๋ค.
๋ฌธ์ ์ ํ๋ก์์ ๋ค(ํ ์ด๋ธ ์ด๋ฆ๊ณผ ์ปฌ๋ผ๋ช ๋ฑ ์ข ๊ฐ์ํจ.)
public static void procedure(){
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sSql = new StringBuffer();
StringBuffer iSql = new StringBuffer();
sSql.append("select * from A");
iSql.append("MERGE INTO ");
iSql.append("B");
iSql.append(" USING db_root ");
iSql.append(" ON no = ? ");
iSql.append(" WHEN MATCHED THEN ");
iSql.append(" UPDATE SET ");
iSql.append(" cd = ? ");
iSql.append(" ,nm = ? ");
iSql.append(" WHEN NOT MATCHED THEN ");
iSql.append(" INSERT ( ");
iSql.append(" no");
iSql.append(" cd");
iSql.append(" ,nm");
iSql.append(" ) VALUES ( ");
iSql.append(" ?,?,? ) ");
try {
conn = ConnectionFactory.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sSql.toString());
while (rs.next()) {
String no = rs.getString("no");
String cd = rs.getString("cd");
String nm = rs.getString("name");
try {
int n = 0;
pstmt = conn.prepareStatement(iSql.toString());
pstmt.setString(++n, no);
pstmt.setString(++n, cd);
pstmt.setString(++n, nm);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs.close();
stmt.close();
conn.close();
}
}
๋ฌธ์ ์ 1.
- pstmt๋ฅผ ์คํํ์์๋ ์๋ฌ๊ฐ ๋ฌ์ ๊ฒฝ์ฐ close ํด์ฃผ๋ ๊ณณ์ด ์๋ค.
๋ฌธ์ ์ 2.
- 5000๊ฑด ์ด์์ ํ๋์ฉ commitํ๊ฒ ๋์ด ์์ ์ด ๋ค๋ก ๋ฐ๋ฆฐ๋ค.
ํด๊ฒฐ๋ฐฉ๋ฒ
public static void procedure(){
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sSql = new StringBuffer();
StringBuffer iSql = new StringBuffer();
sSql.append("select * from A");
iSql.append("MERGE INTO ");
iSql.append("B");
iSql.append(" USING db_root ");
iSql.append(" ON no = ? ");
iSql.append(" WHEN MATCHED THEN ");
iSql.append(" UPDATE SET ");
iSql.append(" cd = ? ");
iSql.append(" ,nm = ? ");
iSql.append(" WHEN NOT MATCHED THEN ");
iSql.append(" INSERT ( ");
iSql.append(" no");
iSql.append(" cd");
iSql.append(" ,nm");
iSql.append(" ) VALUES ( ");
iSql.append(" ?,?,? ) ");
try {
conn = ConnectionFactory.getConnection();
stmt = conn.createStatement();
pstmt = conn.prepareStatement(iSql.toString());
rs = stmt.executeQuery(sSql.toString());
int i = 0;
while (rs.next()) {
String no = rs.getString("no");
String cd = rs.getString("cd");
String nm = rs.getString("name");
try {
int n = 0;
pstmt = conn.prepareStatement(iSql.toString());
pstmt.setString(++n, no);
pstmt.setString(++n, cd);
pstmt.setString(++n, nm);
pstmt.addBatch();
pstmt.clearParameters();
i++;
//OutOfMemory๋ฅผ ๊ณ ๋ฌํด ๋ฐฑ๊ฑด๋จ์ ์ปค๋ฐ
if (i % 100 == 0) {
//batch ์คํ
pstmt.executeBatch();
//batch ์ด๊ธฐํ
pstmt.clearBatch();
}
} catch (Exception e) {
e.printStackTrace();
}
//์ปค๋ฐ๋์ง์์ ๋๋จธ์ง ๊ตฌ๋ฌธ ์ปค๋ฐ
pstmt.executeBatch();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) try {pstmt.close();pstmt = null;} catch(SQLException ex){}
if (conn != null) try {conn.close();conn = null;} catch(SQLException ex){}
if (stmt != null) try {stmt.close();stmt = null;} catch(SQLException ex){}
}
}
๋ฐฉ๋ฒ 1. executeUpdate ๋ฐฉ๋ฒ์ Batch๋ฅผ addํ์ฌ executeํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ณ๊ฒฝ
๋ฐฉ๋ฒ 2. out of memory๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด 100๊ฑด๋จ์๋ก ๋ฐฐ์น๋ฅผ ์ปค๋ฐ. (๊ธฐ์กด์ ํ๊ฑด๋จ์๋ก ์ปค๋ฐํ์ฌ ์๊ฐ์์)
'๊ฐ๋ฐ > SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] ํ ์ด๋ธ ์์ ํน์ ์ปฌ๋ผ ๋ด ๊ฐ์ฅ ๊ธธ์ด ๊ธด ๊ฐ ์ฐพ๊ธฐ (0) | 2022.10.12 |
---|---|
[IBatis] prepend์ and๊ฐ ์๋ถ์๋ (0) | 2022.10.11 |
[CUBRID] DB ์ธ์ด ์ ๋ณ๊ฒฝํ๊ธฐ (0) | 2022.05.11 |
[postgresql] psql๋ก ํ ์ด๋ธ dump exportํ๊ธฐ (0) | 2022.01.13 |
[Postgresql] ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ CSVํ์ผ๋ก ์ถ์ถํ๋ ๋ฐฉ๋ฒ (0) | 2022.01.06 |
Comments