From the comments, i get the impression it is mainly doing the 20000 update statments that is taking too long. Are you running the updates in autocommit mode (default)? if so, then you can improve performance a lot by doing:
START TRANSACTION;
...update statements go here...
COMMIT;
please note that if one of the updates does not succeed due to a constraint violation, you cannot COMMIT. And your data will not be stored permanently until you do COMMIT.
In java, you could do something like:
java.sql.Connection conn = DriverManager.getConnection(connectString, userName, passWord);
//explicit transaction by disabling autocommit.
conn.setAutocommmit(false);
java.sql.Statement stmt = conn.createStatement();
//add all your statements to the batch, f.e. in a loop
for (...) {
stmt.addBatch(sql);
}
int results[] = stmt.executeBatch();
...check the results here...
//store results permanently in db
conn.commit();