Menu

Performance of subsequent query with the same table

Help
Sophia
2017-07-27
2017-07-27
  • Sophia

    Sophia - 2017-07-27

    Hi,
    I am trying to update approx. 3800 rows in a table in two steps:
    1. set all values to 0
    2. set some of them in a loop to a calculated value
    The first step is fast (< 1 s). However the second step takes around 3 s. Maybe there is some problem in the code, because if I run the code without the first step or run the first query on another table, the second step is very fast, too (< 0.1 s). It is slow only when executed after the first step on the same table.
    Here is some code:

    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    conn = DriverManager.getConnection("jdbc:ucanaccess://" + path + ";singleconnection=true" ,"", ""); 
    
    // First update (reset all values)
    long start = System.currentTimeMillis();         
    PreparedStatement ps2 = conn.prepareStatement("UPDATE mytab SET val = 0.0;"); 
    ps2.executeUpdate();
    ps2.close();
    System.out.println("Update 1: " + (System.currentTimeMillis()-start)/1000.0 + " s");
    
    // Second update
    start = System.currentTimeMillis();
    conn.setAutoCommit(false);
    int count = 0;
    PreparedStatement ps = conn.prepareStatement("UPDATE mytab SET val = ? WHERE id = ?;");
    
    for(int j= 1; j <= 3600; j++){
          double value;
          // calculate some value ....
          value = 1.3;
          // update table under certain conditions
          if(true){
                  ps.setDouble(1, value);
                  ps.setInt(2, j);
                  ps.addBatch();
                  count++;
           }
           if(count > 200){
                   ps.executeBatch();
                   count = 0;
            }
    }
    ps.executeBatch();
    conn.commit();
    System.out.println("Update 2: " + (System.currentTimeMillis()-start)/1000.0 + " s");
    

    How to avoid this? Maybe some mistake?
    Thank you for any advice!

     
  • Gord Thompson

    Gord Thompson - 2017-08-22

    This issue is being addressed on Stack Overflow.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.