繁体   English   中英

如何用大表迭代查询

[英]How to iterate a query with a large table

结合使用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行。

尝试使用setFetchSize(int)方法。 有关此链接的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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