2013-01-23 84 views
3

我們現在面臨的一個問題是處理從數據庫中獲取的大ResultSet的分頁。使用jdbcTemplate進行分頁

當前作爲SQL存儲過程返回的行調用jdbcTemplate.query範圍從100K到300K,分頁在extractData方法中完成。

用戶顯示的頁面只有20行。

這是非常緩慢的,有沒有辦法從jdbcTemplate存儲過程的結果中獲取數據頁面。

回答

1

我相信JdbcTemplate沒有用於分頁的特定功能。但是,即使這樣做可能無法幫助您的代碼運行得更快。

通常情況下,面向用戶頁面的大小不會超過200行,因此查詢100-300K行看起來過多並浪費大量內存。

您需要先決定使用哪種分頁策略。兩種常見的策略是查詢前N個頁面,並將其存儲在臨時緩存中 - 或僅查詢足以填充一個頁面大小(例如:200行),並且只有在用戶請求時才查詢下一個200行。

您還需要確定什麼是緩慢的真正原因。行大小是一個因素,但不是唯一的因素。您必須分析您的架構結構,索引,查詢連接等。

請注意,在正常使用情況下 - 雖然您可以向用戶展示多達10000頁,但典型用戶不太可能會經歷所有這些網頁 - 也許只有第5-10頁是重要的 - 因此,如果你能,限制了結果集少數會更有意義

+0

用戶顯示的頁面只有20行。 – xybrek

1

你可以看看this

一個很好的例子開始。

+1

錯誤404 - 鏈接 – xybrek

0

這已經很晚了,但SpringBoot的新版本知道了。

採取在外觀PageImpl

/** 
* Basic {@code Page} implementation. 
* 
* @param <T> the type of which the page consists. 
* @author Oliver Gierke 
*/ 
public class PageImpl<T> extends Chunk<T> implements Page<T> 

構造:

/** 
* Constructor of {@code PageImpl}. 
* 
* @param content the content of this page, must not be {@literal null}. 
* @param pageable the paging information can be {@literal null}. 
* @param total the total amount of items available. The total might be adapted considering the length of the content 
*   given, if it is going to be the content of the last page. This is in place to mitigate inconsistencies 
*/ 
public PageImpl(List<T> content, Pageable pageable, long total) 

希望這有助於!