简体   繁体   中英

How to iterate a query with a large table

Using Java with JDBC
Querying data from a postgres database that stores ship information.
I'm creating a vessel object from the data and adding coordinate data to the vessel object's coordinate list.

    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);
                        }
                    }

So I'm working with a table that contains a billion rows and it'll be impossible to store that many vessel objects on my machine.

I'm wondering how to iterate through the database by querying 1,000,000 rows at a time with each iteration I'll have enough information to run some methods and store important keys, then clear my vessels tree map and run the next 1,000,000 rows.

Try to use setFetchSize(int) method. More about it in this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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