简体   繁体   中英

JDBC MySQL DML Statement Insert with Nested Select Performance

FYI - I'm not a developer, but write code when I have to :) Trying to write some java code to update a database in a batched fashion for multiple records. As I'm inserting new rows, I'm querying another table to find relevant data to add relevant date.

The code seems to work, but my problem is performance. I'm seeing that the full batch of dml statements take about 1 second per statement to execute. I'm updating several thousand records, so this job will take quite awhile to execute. So, what I'm looking for is any other ideas on how I can do this while maximizing performance.

Here's what I'm doing right now.

for(Referrer_UpdateSet i : referrerUpdateSet)
        {


            String dmlStatement = "INSERT INTO TempRefURL (firstTouchDate) " +
            "(SELECT activityDateTime as firstTouch "+ 
            "FROM referrer_URL_backup_10292014 "+ 
            "WHERE mktPersonId = ? "+ 
            "ORDER BY activityDateTime ASC LIMIT 1)";
            stmt = mktoUTMConn.prepareStatement(dmlStatement);
            stmt.setInt(1, i.id);
            //System.out.println(stmt+" \n");
            stmt.executeUpdate();
        }
        mktoUTMConn.commit();

I'm also trying preparedStatements.addBatch, but it doesn't seem to be working (only 1 row inserted..)

        System.out.println("updating temp table with referrer URL data");
        //iterate through array of parsed referrer URLs

        String dmlStatement = "UPDATE dml_sandbox.TempRefURL SET Referrer_URL = ? " + "WHERE id = ?";
        for(Referrer_UpdateSet i : referrerUpdateSet){




            stmt = mktoUTMConn.prepareStatement(dmlStatement);
            stmt.setInt(2, i.id);
            stmt.setString(1, i.cleanURL);
            //System.out.println(stmt+" \n");
            stmt.addBatch();
            //stmt.executeUpdate();


            //System.out.println(stmt+" \n");
        }   
        stmt.executeBatch();
        System.out.println("Done updating temp table with referrer URL data");
        mktoUTMConn.commit();

Any suggestions would be greatly appreciated. Thanks!

Simple fix. See my comment above. Here's the new code:

String dmlStatement = "UPDATE dml_sandbox.TempRefURL SET Referrer_URL = ? " + "WHERE id = ?";
        stmt = mktoUTMConn.prepareStatement(dmlStatement);
        //iterate through array of parsed referrer URLs
        for(Referrer_UpdateSet i : referrerUpdateSet){

            stmt.setInt(2, i.id);
            stmt.setString(1, i.cleanURL);
            stmt.addBatch();
            stmt.executeUpdate();

        }   
        System.out.println(stmt+" \n");
        int[] recordsAffected = stmt.executeBatch();
        System.out.println("Done updating temp table with referrer URL data");
        System.out.println(recordsAffected.length +  " records affected");
        mktoUTMConn.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM