开发者

Multiple SQL updates best practice

I'm considering three alternatives to the seemingly simple task of updating multiple rows in a table. I'd like to know which one is best in terms of efficiency and code readability. Though I'm not sure there is a clear answer to the question on efficiency, which may be dependent upon the database being used.

In my particular case I'm using PostgresSQL as the database, and the table in question contains under one million entries. I have a process right now that is responsible for pulling out a subset of data from this table, performing some work, and then updating those rows with a timestamp.

As I see it I have three options:

  1. Construct a single update statement using the in clause, the contents of which will be replaced with a constructed string containing the ids of rows to update.

    String update = "update mytable set mydate = now() where id in (?)";
    StringBuilder ids = new StringBuilder();
    
    for (Item item : items) { 
        ids.append(item.getId()).ap开发者_开发问答pend(","); 
    }
    
    ids.setLength(ids.length() - 1);
    update.replace("?", ids.toString());    
    ps = con.prepareStatement(update);
    ps.executeUpdate();
    
  2. Use a batch

    ps = con.prepareStatement("update mytable set mydate = now() where id = ?");
    
    for (Item item : items) { 
        ps.setInt(1, item.getId());
        ps.addBatch();
    }
    
    ps.executeBatch();
    
  3. Use a transaction

    con.setAutoCommit(false);
    ps = con.prepareStatement("update mytable set mydate = now() where id = ?");
    
    for(Item item : items) { 
        ps.setInt(1, item.getId());
        ps.executeUpdate();
    }
    
    con.commit();
    

Which of these would be considered the best practice? Or is there another way you would propose? I personally like option number two, it's concise and easy to ready, but my gut feeling is that option number one will be the most performant.


Thoughts on your suggestions
The transaction has the distinct advantage of allowing the update to be rolled back.

A constructed SQL statement holds the risk of breakage, also note that you are sending a lot of data across the wire.

Alternative suggestions

join against a temp-table I would create a temp-table holding all the values to update and then construct a update-statement with a join against the temp-table to update.

use copy
If it's raw speed you are after I would suggest the copy statement, see:
http://wiki.postgresql.org/wiki/COPY

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜