使用nosql dynamodb表中的分区键查找总记录数的复杂性? [英] Complexity of finding total records count with partition key in nosql dynamodb table?

查看:156
本文介绍了使用nosql dynamodb表中的分区键查找总记录数的复杂性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计DynamoDB数据库表。在一个表中,例如 test_table ,我定义了一个组合键,其中包含一个分区键,例如 partition_id 和排序键,例如 sort_id partition_id sort_id 可能在它们的条目中都有一些重复的值,但是两者都组合在一起。 partition_id 和 sort_id 一起永远都是唯一的。

I am designing a DynamoDB database tables. In one table, say test_table, I have defined a composite key with the combination of a partition key , say partition_id and a sort key, say sort_id. Both partition_id and sort_id may be have some duplicate values in their entries but the combination of both partition_id and sort_id together will always be unique.

我想知道,具有 partition_id 的总记录的检索效率如何,例如 partition_id = x

I am interested to know, how efficient is the retrieval of total records having some partition_id, say partition_id = x ?

推荐答案

您的实际答案问题是……效率很低。

The answer to your actual question is...very inefficient.

与RDBMS不同,DDB没有 COUNT 或任何其他聚合函数...因此获取计数的唯一方法是 Query() Scan()您的数据,将该数据返回到您的应用程序中,然后自己对行进行计数。

Unlike a RDBMS, DDB doesn't have a COUNT or any other aggregation functions...so the the only way to get a count is to Query() or Scan() your data, returning that data to your application and counting the rows yourself.

如果您需要聚合,则最佳做法是启用DDB流并将Lambda与其绑定

Best practice if you need aggregates, would be to enable DDB Streams, and tie a Lambda to it that would calculate and store the aggregate (count) you're interested in.

因此,在您询问的情况下,

So in the case you ask about,


partition_id = x

partition_id = x

您可以在表中添加另一行(pk = X,sk = COUNT),lambda将在每个ti上更新该行我插入或删除了 pk = x 的记录。

You could add another row to your table, (pk=X, sk="COUNT") and the lambda would update that row every time a record with pk=x is inserted or deleted.

然后,您可以通过简单的方法有效地获得计数 GetItem(pk = X,sk = COUNT)

Then you can efficiently get the count with a simple GetItem(pk=X, sk="COUNT")

文档中的更多信息对物化聚集查询使用全局二级索引

这篇关于使用nosql dynamodb表中的分区键查找总记录数的复杂性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆