Play 2 框架 2.2.4 (ebean):分页在调用 Page<>.getList() 时每页生成多个 sql 查询
Play 2 framework 2.2.4 (ebean): pagination generates mutiple sql queries per one page when call Page<>.getList()
我们在项目中使用 Play 2 Framework 2.2.4(集成了 eBean ORM)。
我们的分页看起来像:
public static Page<Users> page(int pageNum, int pageSize) {
Page<Users> page = find.where().findPagingList(pageSize).getPage(pageNum);
return page;
}
...
Page<User> currentPage = page(0, 10); // for example
然后在视图中:
@for(user <- currentPage.getList) {
// display information for every user
...
}
当我打开将 SQL 查询记录到控制台时,我感到很惊讶 - 为了获取一页的用户列表,Ebean 对 每一页[=31] 进行了 SQL 请求=]!
对于我们的 329 个用户,这意味着 33 个 SQL 查询:
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11 offset 10
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11 offset 20
...
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11 offset 320
通过一些实验我发现,大量 SQL 查询恰好在方法 Page<T>.getList()
调用时出现。即使它被单独调用而没有任何其他操作。
我不明白 - 所有这些查询的原因是什么?
这是某种错误吗?
玩2.2.4 uses Ebean version 3.2.2, and this version of Ebean uses a "fetch ahead" strategy调用getPage
.
获得所有页面
您可以通过将提前获取 属性 设置为 false 来更改此设置,如下所示:
public static Page<Users> page(int pageNum, int pageSize) {
Page<Users> page = find.where()
.findPagingList(pageSize)
.setFetchAhead(false)
.getPage(pageNum);
return page;
}
List<Notification> notifications = Ebean.find(Notification.class).where()
.eq("notification_belong_to_user_id", userId)
.order().desc("notificationDate")
.findPagedList(pageNo, (int) pageSize)
.getList();
我们在项目中使用 Play 2 Framework 2.2.4(集成了 eBean ORM)。 我们的分页看起来像:
public static Page<Users> page(int pageNum, int pageSize) {
Page<Users> page = find.where().findPagingList(pageSize).getPage(pageNum);
return page;
}
...
Page<User> currentPage = page(0, 10); // for example
然后在视图中:
@for(user <- currentPage.getList) {
// display information for every user
...
}
当我打开将 SQL 查询记录到控制台时,我感到很惊讶 - 为了获取一页的用户列表,Ebean 对 每一页[=31] 进行了 SQL 请求=]!
对于我们的 329 个用户,这意味着 33 个 SQL 查询:
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11 offset 10
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11 offset 20
...
[debug] c.j.b.PreparedStatementHandle - select t0.user_id c0, t0.user_name c1, t0.first_name c2, t0.last_name c3, t0.user_full_name c4, t0.sex_id c5, t0.status c7, t0.role_id c8, t0.phone c9, t0.email c10 from users t0 order by user_id
limit 11 offset 320
通过一些实验我发现,大量 SQL 查询恰好在方法 Page<T>.getList()
调用时出现。即使它被单独调用而没有任何其他操作。
我不明白 - 所有这些查询的原因是什么?
这是某种错误吗?
玩2.2.4 uses Ebean version 3.2.2, and this version of Ebean uses a "fetch ahead" strategy调用getPage
.
您可以通过将提前获取 属性 设置为 false 来更改此设置,如下所示:
public static Page<Users> page(int pageNum, int pageSize) {
Page<Users> page = find.where()
.findPagingList(pageSize)
.setFetchAhead(false)
.getPage(pageNum);
return page;
}
List<Notification> notifications = Ebean.find(Notification.class).where()
.eq("notification_belong_to_user_id", userId)
.order().desc("notificationDate")
.findPagedList(pageNo, (int) pageSize)
.getList();