Question : How to optimize this scenario? Four update statements in a loop of more than 67000 records

How can i update the following code?

It takes almost 10 hrs or so to complete execution.

Sometimes it also gives me "Unable to extend Undo Tables pace" error message also

Undo Tablespace is 1GB in size.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
// Deleting all records from imal_account_temp
        String tempAccountDelete = "delete from imal_account_temp";
        statement = connection.prepareStatement(tempAccountDelete);
        statement.executeUpdate();
        
        // Inserting records from imal_account into imal_account_temp
        String tempAccount = "insert into imal_account_temp(ROWNUMBER, BRANCHCODE, CY_CODE, GL_CODE, CIF_SUB_NO) " +
                             " select ROWNUMBER, BRANCHCODE, CY_CODE, GL_CODE, CIF_SUB_NO from imal_account ";
        
        //System.out.println(""+tempAccount);
        statement = connection.prepareStatement(tempAccount);
        statement.executeUpdate();
        
        connection.commit();
        
        String accountForUpdateQuery = "SELECT ROWNUMBER, brm_code, BRANCHCODE, CY_CODE, GL_CODE, " +
                                        " CIF_SUB_NO, SL_NO, ACCT_BASC, ACCT_SFIX, " +
                                        " NOMN_ACCT_NUMB, NOMN_ACCT_SFIX, FUND_ACCT_NUMB, FUND_ACCT_SFIX" +
                                        " from imal_account ";
        statement = connection.prepareStatement(accountForUpdateQuery);
        result    = statement.executeQuery();
        
        String tempQuery="";
        ResultSet resultTemp=null;
        
        while(result.next()) {
            PreparedStatement  statementCurrent= null;
            
            //Updating GL_CODE now
            accountUpdateQuery = "update imal_account a " +
                    " set a.GL_CODE = ( select IMAL_ACCOUNT from imal_account_map " +
                    "                   where ACCT_TYPE = a.ACCT_TYPE and rownum = 1) " +
                    " where a.ROWNUMBER = " + result.getLong("ROWNUMBER") ;
            
            //DBManager.logMessage("3>> "+accountUpdateQuery);
            statementCurrent = connection.prepareStatement(accountUpdateQuery);
            statementCurrent.executeUpdate();
            statementCurrent.close();
            
            //Updating PFT_GL now
            accountUpdateQuery = "update imal_account a " +
                    " set a.PFT_GL = ( select IMAL_ACCOUNT from imal_account_map " +
                    "                   where ACCT_TYPE = (select acct_type " +
                    "                                       from pls.md " +
                    "                                       where BRAN_CODE = " + result.getLong("BRANCHCODE") +
                    "                                       and ACCT_BASC = " + result.getLong("NOMN_ACCT_NUMB") +
                    "                                       and ACCT_SFIX = " + result.getLong("NOMN_ACCT_SFIX") +
                    "                                       ) " +
                    "                   ) " +
                    " where a.ROWNUMBER = " + result.getLong("ROWNUMBER") ;
            
            //DBManager.logMessage("4>> "+accountUpdateQuery);
            statementCurrent = connection.prepareStatement(accountUpdateQuery);
            statementCurrent.executeUpdate();
            statementCurrent.close();
            
            //Updating RENEW_GL now
            accountUpdateQuery = "update imal_account a " +
                    " set a.RENEW_GL = ( select IMAL_ACCOUNT from imal_account_map " +
                    "                   where ACCT_TYPE = (select acct_type " +
                    "                                       from pls.md " +
                    "                                       where BRAN_CODE = " + result.getLong("BRANCHCODE") +
                    "                                       and ACCT_BASC = " + result.getLong("FUND_ACCT_NUMB") +
                    "                                       and ACCT_SFIX = " + result.getLong("FUND_ACCT_SFIX") +
                    "                                       ) " +
                    "                   ) " +
                    " where a.ROWNUMBER = " + result.getLong("ROWNUMBER") ;
            
            //DBManager.logMessage("5>> "+accountUpdateQuery);
            statementCurrent = connection.prepareStatement(accountUpdateQuery);
            statementCurrent.executeUpdate();
            statementCurrent.close();
            
            //Checking if this record already exist in imal_account_temp table
            tempQuery = "select ROWNUMBER, BRANCHCODE, CY_CODE, GL_CODE, CIF_SUB_NO from imal_account_temp " +
                    " where BRANCHCODE  = " + result.getLong("BRANCHCODE") +
                    " and CY_CODE  = " + result.getLong("CY_CODE") +
                    " and GL_CODE  = " + result.getLong("GL_CODE") +
                    " and CIF_SUB_NO  = " + result.getLong("CIF_SUB_NO") +
                    " and SL_NO is null ";
            
            statementCurrent = connection.prepareStatement(tempQuery);
            resultTemp       = statementCurrent.executeQuery();
            
            //if record exists then
            int counter = 0;
            while(resultTemp.next()) {
                PreparedStatement  statementTemp= null;
                counter = counter + 1;
                tempQuery = "update imal_account_temp set SL_NO = " + counter +
                        " where ROWNUMBER = " + resultTemp.getLong("ROWNUMBER") +
                        " and BRANCHCODE  = " + resultTemp.getLong("BRANCHCODE") +
                        " and CY_CODE  = " + resultTemp.getLong("CY_CODE") +
                        " and GL_CODE  = " + resultTemp.getLong("GL_CODE") +
                        " and CIF_SUB_NO  = " + resultTemp.getLong("CIF_SUB_NO") ;
                
                statementTemp = connection.prepareStatement(tempQuery);
                statementTemp.executeUpdate();
                statementTemp.close();
            }
            statementCurrent.close();
        }
        connection.commit();
        
        // Updating SL_NO in imal_account table from imal_account_temp
        accountUpdateQuery = "update imal_account a " +
                " set a.SL_NO = (select b.SL_NO " +
                " from imal_account_temp b " +
                " where a.ROWNUMBER = b.ROWNUMBER) " ;
        
        //System.out.println(""+accountUpdateQuery);
        statement = connection.prepareStatement(accountUpdateQuery);
        statement.executeUpdate();
        statement.close();
        
        connection.commit();
Open in New Window Select All

Answer : How to optimize this scenario? Four update statements in a loop of more than 67000 records

yes, the problem is the row-by-row processing

try this version,  note I commented out all of the COMMIT lines so you could test it and verify the results safely.  Once you've confirmed it does work correctly (I think it will) you can put the commits back in.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
CREATE OR REPLACE PROCEDURE acc_slno_counter
AS
BEGIN
    imal_migration.log_message (p_message       => 'ACC_SLNO_COUNTER - Starting...');
 
    UPDATE imal_account a
       SET gl_code = (SELECT imal_account
                        FROM imal_account_map
                       WHERE acct_type = a.acct_type AND ROWNUM = 1),
           pft_gl =
               (SELECT imal_account
                  FROM imal_account_map
                 WHERE acct_type =
                           (SELECT acct_type
                              FROM pls.md
                             WHERE bran_code = a.branchcode
                               AND acct_basc = a.nomn_acct_numb
                               AND acct_sfix = a.nomn_acct_sfix)),
           renew_gl =
               (SELECT imal_account
                  FROM imal_account_map
                 WHERE acct_type =
                           (SELECT acct_type
                              FROM pls.md
                             WHERE bran_code = a.branchcode
                               AND acct_basc = a.fund_acct_numb
                               AND acct_sfix = a.fund_acct_sfix));
 
  --  COMMIT;
    imal_migration.log_message
        (p_message       => 'ACC_SLNO_COUNTER - 1st batch of updates finished, now executing 2nd batch of updates...'
        );
 
    UPDATE imal_account_temp
       SET sl_no = ROWNUM - 1
     WHERE sl_no IS NULL
       AND (rownumber, branchcode, cy_code, gl_code, cif_sub_no) IN (
                    SELECT rownumber, branchcode, cy_code, gl_code,
                           cif_sub_no
                      FROM imal_account);
 
    --COMMIT;
    imal_migration.log_message
                         (p_message       => 'ACC_SLNO_COUNTER - Updating SL_NO now');
 
    -- Updating SL_NO in imal_account table from imal_account_temp
    UPDATE imal_account a
       SET a.sl_no = (SELECT b.sl_no
                        FROM imal_account_temp b
                       WHERE a.rownumber = b.rownumber);
 
    --COMMIT;
    imal_migration.log_message (p_message => 'ACC_SLNO_COUNTER - Finished');
EXCEPTION
    WHEN OTHERS
    THEN
        imal_migration.log_errors (p_error_message       =>    'ACC_SLNO_COUNTER: '
                                                            || SQLERRM
                                  );
        raise_application_error
                           (-20001,
                               'ACC_SLNO_COUNTER: An error was encountered - '
                            || SQLCODE
                            || ' - ERROR - '
                            || SQLERRM
                           );
END acc_slno_counter;
Open in New Window Select All
Random Solutions  
 
programming4us programming4us