2013-01-23 85 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) 

希望这有助于!