問題我現在有被jdbcTemplate
調用的存儲過程返回大量記錄,百萬條記錄,這使得我們的java查詢方法非常緩慢。使用jdbcTemplate處理大型結果集
我的java查詢方法執行結果分頁,所以它非常緩慢。如何在不更改DB存儲過程的情況下改進此功能。也就是說,只查詢特定的行,這樣java方法就不會有處理百萬條記錄的負擔來做分頁。
public PageModel<Map<String, String>> query(String sql, final int offset, final int limit) throws ReportException {
try {
return jdbcTemplate.query(sql, new ResultSetExtractor<PageModel<Map<String, String>>>() {
@Override
public PageModel<Map<String, String>> extractData(ResultSet rs)
throws SQLException, DataAccessException {
long startTime = System.nanoTime();
PageModel<Map<String, String>> pageModel = new PageModel<Map<String,String>>();
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
int rows = 0;
// skip rows
for (int i=0; i<offset&&rs.next(); i++) {
rows++;
}
// get rows
for (int i=0; i<limit&&rs.next(); i++) {
Map<String, String> map = new HashMap<String, String>();
ResultSetMetaData metadata = rs.getMetaData();
int count = metadata.getColumnCount();
for (int j=1; j<=count; j++) {
map.put(metadata.getColumnName(j), rs.getString(j));
}
list.add(map);
rows++;
}
// iterate remaining rows to get total rows
while (rs.next())
rows++;
pageModel.setOffset(offset);
pageModel.setLimit(limit);
pageModel.setData(list);
pageModel.setTotal(rows);
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("Query took: " + duration);
return pageModel;
}
});
} catch (DataAccessException e) {
throw new ReportException(e);
}
}
爲什麼你需要查詢的記錄一百萬在一個回合? – 2013-01-23 09:30:14