๐ŸŒท๐ŸŒผ๋ชจ์—ฌ๋ด์š” ๊ฐœ๋ฐœ์˜์ˆฒ๐ŸŒท๐ŸŒผ

[ํ๋ธŒ๋ฆฌ๋“œ] ํ”„๋กœ์‹œ์ € ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๋ณธ๋ฌธ

๊ฐœ๋ฐœ/SQL

[ํ๋ธŒ๋ฆฌ๋“œ] ํ”„๋กœ์‹œ์ € ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜

์š”์ผ์ด 2022. 7. 28. 16:00
๋ฐ˜์‘ํ˜•

์–ด๋Š๋‚  ํ…Œ์ด๋ธ”์— 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๊ฑด๋‹จ์œ„๋กœ ๋ฐฐ์น˜๋ฅผ ์ปค๋ฐ‹. (๊ธฐ์กด์—” ํ•œ๊ฑด๋‹จ์œ„๋กœ ์ปค๋ฐ‹ํ•˜์—ฌ ์‹œ๊ฐ„์†Œ์š”)

 

Comments