Fastest way to iterate through large table using JDBC

前端 未结 3 1130
名媛妹妹
名媛妹妹 2020-12-07 23:07

I\'m trying to create a java program to cleanup and merge rows in my table. The table is large, about 500k rows and my current solution is running very slowly. The first thi

3条回答
  •  爱一瞬间的悲伤
    2020-12-07 23:46

    First of all, are you sure you need the whole table in memory? Maybe you should consider (if possible) selecting rows that you want to update/merge/etc. If you really have to have the whole table you could consider using a scrollable ResultSet. You can create it like this.

    // make sure autocommit is off (postgres)
    con.setAutoCommit(false);
    
    Statement stmt = con.createStatement(
                       ResultSet.TYPE_SCROLL_INSENSITIVE, //or ResultSet.TYPE_FORWARD_ONLY
                       ResultSet.CONCUR_READ_ONLY);
    ResultSet srs = stmt.executeQuery("select * from ...");
    

    It enables you to move to any row you want by using 'absolute' and 'relative' methods.

提交回复
热议问题