LIMIT
PreparedStatementを使用してクエリを変更することをお勧めします。何かのようなもの:
SELECT * FROM table1 LIMIT ?,?
これにはいくつかの利点があります。
- すべてを一度にフェッチしているわけではありません。テーブルで処理する行が多い場合は、パフォーマンスが向上することがあります。
- バッチごとにフェッチする要素の数を事前に定義するように変更できます
したがって、コードは次のようになります。
PreparedStatement ps = null;
ResultSet rs = null;
final int FETCH_LIMIT = 5; //number of elements to fetch per batch
final int BATCH_LIMIT = 3; //number of batches you would want
int currentRows = 0;
try{
ps = connection.prepareStatement("SELECT * FROM table1 LIMIT ?,?");
for(int currentBatch = 0; currentBatch < BATCH_LIMIT; currentBatch++){
ps.clearParameters();
ps.setInt(1, currentRows);
ps.setInt(2, currentRows + FETCH_LIMIT);
try{
rs = ps.executeQuery();
while(rs.next()){
// do your work
}
}catch(Exception exe){
//manage exception
}finally{
//manage resultset
}
currentRows += FETCH_LIMIT;
}
}catch(Exception exe){
//Handle your exception
}
finally{
//Manage your resources
}