简体   繁体   English

如果我们设置了限制,如何获取 DynamoDB 中的总页数?

[英]How to get total number of pages in DynamoDB if we set a limit?

I have a list of documents that are stored in DynamoDB .我有一个存储在DynamoDB中的文档列表。 There are more than 10,000 documents being stored in the db.数据库中存储了10,000 多个文档。 I have set the limit to 100 when requesting.请求时我已将限制设置为100 DynamoDB is doing a good job by returning me the first 100 documents and the LastEvaluatedKey to get the next 100 documents. DynamoDB通过返回前100 个文档和LastEvaluatedKey来获取接下来的100文档,做得很好。

The problem here is I also want the DynamoDB to return me the total number of pages for pagination purpose.这里的问题是我还希望DynamoDB返回我用于分页的总页数。 In this case since i have 10,000 documents it should return 100 (the number of pagination)在这种情况下,由于我有10,000 个文档,它应该返回100 (分页数)

For now what I have done is that I have been counting manually by looping the queries until it doesn't return me the LastEvaluatedKey .现在我所做的是通过循环查询来手动计数,直到它不返回LastEvaluatedKey Add up how many looping has been done to get the total page.将完成的循环次数相加以获得总页数。 But I believe there are better approach.但我相信有更好的方法。

Neither " Scan " (to read the entire table) nor " Query " (to read all the items with the same partition key) operations return an estimate on how many total results there are. Scan ”(读取整个表)和“ Query ”(读取具有相同分区键的所有项目)操作都不会返回对总结果数的估计。 In some cases (eg, when a FilterExpression is provided), there is no efficient way for DynamoDB to do this estimation.在某些情况下(例如,当提供FilterExpression时),DynamoDB 没有有效的方法来进行这种估计。 In other cases there may be a way for DynamoDB to provide this information, but it doesn't.在其他情况下,DynamoDB 可能有办法提供此信息,但事实并非如此。

If I understand you correctly, you want to Scan the entire table, without a filter.如果我理解正确,您想Scan整个表格,而无需过滤器。 Like I said, Scan itself doesn't give you the number of items in the table.就像我说的, Scan本身不会给你表中的项目数。 But you can find this number using DescribeTable , which returns, among other things, an " ItemCount " attribute, which is an estimate on the total number of items in the table, which may not be completely up-to-date but perhaps is good enough for your needs (if you want an estimate for some sort of progress report, that doesn't need to be 100% accurate).但是你可以使用DescribeTable找到这个数字,它返回一个“ ItemCount ”属性,它是对表中项目总数的估计,它可能不是完全最新的,但可能是好的足以满足您的需求(如果您想要对某种进度报告进行估算,则不需要 100% 准确)。

If you really need accurate and up-to-the-moment counts of items in a partition or an entire table, you can always try to maintain such counters as separate data.如果您确实需要对分区或整个表中的项目进行准确和最新的计数,您始终可以尝试将此类计数器作为单独的数据进行维护。 Doing this correctly is not trivial, and will have performance implications, but in some use cases (eg, rare writes and a lot of reads) may be useful.正确执行此操作并非易事,并且会影响性​​能,但在某些用例中(例如,很少写入和大量读取)可能有用。

As the other answer has correctly explained, there is no efficient way to get total result counts for DynamoDB query or scan operations.正如另一个答案已正确解释的那样,没有有效的方法来获取 DynamoDB 查询或扫描操作的总结果计数。 As such there is no efficient way to get total number of pages.因此,没有获得总页数的有效方法。

However, what I wanted to call out is that modern UIs have been moving away from classic pagination towards an infinite scroll design pattern.然而,我想指出的是,现代 UI 已经从经典分页转向无限滚动设计模式。 Where the “next page” of results is loaded on demand as the List is scrolled.在滚动列表时按需加载结果的“下一页”。 This can be achieved with DynamoDB.这可以通过 DynamoDB 实现。 You can still show discrete pages but cannot show, apriori, how many results or how many pages there are.. It's a current shortcoming of DynamoDB.您仍然可以显示离散页面,但不能先验地显示有多少结果或有多少页面。这是DynamoDB 当前的一个缺点。

您可以使用 DynamoDB 流维护自己的计数——基本上,您创建一个 Lambda 函数来监视正在创建/删除的项目,然后写回 DynamoDB 中存储当前项目计数的计数器项目。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM