如何使用大表迭代查询

时间:2015-06-08 12:57:40

标签: java postgresql jdbc

将Java与JDBC一起使用
查询存储船舶信息的postgres数据库中的数据。我从数据中创建一个血管对象,并将坐标数据添加到血管对象的坐标列表中。

    ResultSet rs = stmt.executeQuery("SELECT mmsi, report_timestamp, position_geom, ST_X(position_geom) AS Long, "+ 
                "ST_Y(position_geom) AS Lat FROM reports3 WHERE position_geom IS NOT NULL ORDER by report_timestamp ASC"); 

 TreeMap <Long, Vessel> vessels = new TreeMap<Long, Vessel>(); 
                    long startTime2 = System.nanoTime();

                    while(rs.next()){
                        long mmsi = rs.getLong("mmsi");
                        java.util.Date time = rs.getTime("report_timestamp"); 
                        double longitude = rs.getDouble("Long");
                        double latitude = rs.getDouble("Lat");
                        Coordinate coordinate = new Coordinate(longitude, latitude, time);
                        Vessel vessel = new Vessel(mmsi); 

                        if(!vessels.containsKey(mmsi)) { //if vessel is not present in vessels
                            vessel.addCoor(coordinate);
                            vessels.put(mmsi, vessel);
                        }
                        else { //if vessel is already in vessels 
                            vessels.get(mmsi).addCoor(coordinate);
                        }
                    }

因此,我正在使用一个包含十亿行的表格,并且无法在我的机器上存储那么多容器对象。

我想知道如何通过每次迭代一次查询1,000,000行来遍历数据库我将有足够的信息来运行某些方法并存储重要的密钥,然后清除我的船只树图并运行下一个1,000,000行。

1 个答案:

答案 0 :(得分:0)

尝试使用setFetchSize(int)方法。 有关此问题的更多信息,请参见link