[英]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.