简体   繁体   English

分页大结果集的最佳方法是什么-Java

[英]what is best way to paging big Resultset -Java

i am looking for best aproach from perfomance point of view , to show Resultset on webpage partially , lets say by 10 item per page and if user want to see more result, he pressing "next" btn .我正在寻找从性能的角度来看最好的方法,在网页上部分显示结果集,让我们说每页 10 个项目,如果用户想要查看更多结果,他按“下一步”btn。 i think (probablly wrong) it should be new request to the Server when "Next" button is pressed ??我认为(可能是错误的)当按下“下一步”按钮时,它应该是对服务器的新请求??

currentlly i trying to learn Java,GWT目前我正在尝试学习 Java,GWT

thank You!谢谢你!

PS: sorry for my English. PS:对不起我的英语。

The answer would depend on your users' behavior: how often will the look at page 2, or page 10, or page 100.答案取决于用户的行为:查看第 2 页、第 10 页或第 100 页的频率。

If they rarely look at page 2, and never look at page 10 or page 100, then resubmitting the request may be fine.如果他们很少看第 2 页,从不看第 10 页或第 100 页,那么重新提交请求可能没问题。

If they usually look at page 2, often look at page 10, and occasionally look at page 100, then a partial cache will be useful: cache the first 100 (or 200, or 300) results, and only resubmit the query when they go past those results.如果他们通常看第 2 页,经常看第 10 页,偶尔看第 100 页,那么部分缓存会很有用:缓存前 100 个(或 200,或 300 个)结果,并仅在它们去时重新提交查询过去的那些结果。 I would probably store the cache in the user's session, although you have to give that some thought if your application server is clustered.我可能会将缓存存储在用户的会话中,尽管如果您的应用程序服务器是集群的,您必须考虑一下。

And if they always page through every result?如果他们总是翻阅每个结果? Partial caches are still the answer, because you don't want to store large chunks of data in-memory.部分缓存仍然是答案,因为您不想在内存中存储大块数据。

Since you have "GWT" in your tags, I'll assume your server app is running on Google App Engine (GAE).由于您的标签中有“GWT”,我假设您的服务器应用程序在 Google App Engine (GAE) 上运行。

  • One approach is to have your first query obtain all results, store them in a DB, show the first 20 and then let the next/prev links pull subsets of the stored data out of the DB.一种方法是让您的第一个查询获得所有结果,将它们存储在数据库中,显示前 20 个,然后让下一个/上一个链接从数据库中提取存储数据的子集。 You must remember to delete those results from the DB when your user's session times out!当您的用户会话超时时,您必须记住从数据库中删除这些结果!

  • Another approach is to obtain all results on each page view, but skip through the results until you've hit the desired subset of 20, and output only those.另一种方法是获取每个页面视图上的所有结果,但跳过结果,直到达到所需的 20 个子集,然后仅输出这些结果。

I think that with GAE underneath, the 2nd approach will work better, unless your query is likely to return more than 1000 results, which GAE won't let you retrieve in one transaction.我认为在下面的 GAE 中,第二种方法会更好,除非您的查询可能返回 1000 多个结果,GAE 不会让您在一个事务中检索这些结果。

  • The best approach, if your data and keys lend themselves to it, would be to pull out the correct 20 data items already at query time.最好的方法是,如果您的数据和键适用于它,则是在查询时提取出正确的 20 个数据项。 But unless your data has continuously ascending integer keys, this may be hard to do.但是除非您的数据具有连续递增的整数键,否则这可能很难做到。

You usually only get a "page" from the database.您通常只能从数据库中获得一个“页面”。

Let's say a query让我们说一个查询

select * from mytable where column1="a";

would give 1000 records.将给出 1000 条记录。 Then getting a page would be like (mysql):然后获取一个页面就像(mysql):

select * from mytable where column1="a" limit 0, 10;

for page 1 (0 to 10), and page 2 would be retrieved like:对于第 1 页(0 到 10),第 2 页将被检索为:

select * from mytable where column1="a" limit 10, 20;

and so forth.等等。 If the data is large (1000 records), but not huge ( 1000 000 records), you can also give the entire dataset at once and use javascript to page.如果数据很大(1000 条记录),但不是很大(1000 000 条记录),您也可以一次给出整个数据集并使用javascript 进行分页。 That has the added advantage that sorting can be done client-side.这具有额外的优势,即可以在客户端进行排序。

If you can't use a cache-based approach due to memory limitations, use a query-based approach.如果由于内存限制而无法使用基于缓存的方法,请使用基于查询的方法。 Adjust the WHERE clause on your search query to explicitly select data based on which page the user has requested.调整搜索查询上的 WHERE 子句,以根据用户请求的页面明确选择数据。 This approach requires that you pass additional context information back and forth on your page requests.这种方法要求您在页面请求中来回传递额外的上下文信息。

One approach is to fetch pages using logical Row IDs (or primary keys) that delimit the page and identify each row in the result set.一种方法是使用逻辑行 ID(或主键)来获取页面,这些逻辑行 ID(或主键)分隔页面并标识结果集中的每一行。

Say you have a very simple table with a numerical sequence of row ids.假设您有一个非常简单的表,其中包含行 ID 的数字序列。 If you are showing 100 rows per page, and the user has requested page two, you would adjust the WHERE clause as follows:如果每页显示 100 行,并且用户请求了第二页,则应按如下方式调整 WHERE 子句:

select col, col2 from my_table where
row_id > 100
and row_id <= 200
order by rownum asc

you can cache/retrieve the records in web layer, backend layer(for example ejb) or the database layer(as last "limit" or row_id statement).您可以缓存/检索 web 层、后端层(例如 ejb)或数据库层(作为最后一个“限制”或 row_id 语句)中的记录。 which approach you should use depends on your requirement(as said by kdgregory).您应该使用哪种方法取决于您的要求(如 kdgregory 所说)。

most popular one is to cache them at the web layer using the session.最流行的一种是使用会话将它们缓存在 web 层。

If you are using JPA (which works quite well on GAE), you can paginate a result set using如果您使用的是 JPA(在 GAE 上效果很好),您可以使用

Query#setFirstResult(int startPosition) 查询#setFirstResult(int startPosition)

Query#setMaxResults(int maxResult) 查询#setMaxResults(int maxResult)

This article might be helpful: Paging large data sets with a LazyList这篇文章可能会有所帮助: Paging large data sets with a LazyList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中创建Statement和ResultSet的最佳方法是什么 - What is the best way to create Statement and ResultSet in java 将结果集映射到对象的最佳方法是什么 - What's the best way to map the resultset to object 在Java中以美分(整数)转换美元(大十进制)的最佳方法是什么? - What is the best way to convert Dollars (Big Decimal) in Cents (Integer) in java? 从非常大的结果集中显示数据的最佳方法是什么? - What is the best way to present data from a very large resultset? 将结果集数据放入文本文件的最佳方法是什么? - what is the best way to put resultset data into a text file? 在 OSS 驱动程序中将 Cassandra ResultSet 转换为 java 对象的最佳方法 - Best way to convert Cassandra ResultSet to java objects in OSS driver 使用 jQuery 或 Java 制作大文件的最佳方法 - the best way to make a big file with jQuery or Java 将大十进制格式转换为美元格式的最佳方法是什么? - What is the best way to format a big decimal into dollar form? 在Intellij中的大项目中重新排列文件的最佳方法是什么? - What is the best way to rearrange files in a big project in Intellij? 解决大数字字符串变长的最佳方法是什么? - what is the best way to solve the big number string to long?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM