在DynamoDB中选择每个组的前N条记录 [英] Selecting top N records per group in DynamoDB
问题描述
通常是NoSQL,尤其是DynamoDB,非常适合执行最大的n-per-group 类型查询,与MySQL相比?
Is NoSQL in general, and DynamoDB in particular, well suited to performing greatest-n-per-group type queries, as compared to MySQL?
推荐答案
DynamoDB仅支持2个索引,并且只能在这些索引上高效地查询。
DynamoDB support only 2 index and can only be queried efficiently on these.
- 哈希键
- 范围键(可选)
- hash key
- range key (optional)
使用DynamoDB在其中查找最大值随机的行根本不是一个好主意。在随机行上查询意味着扫描整个数据集,这将花费您很多钱。
Using DynamoDB to find the biggest values in a random "row" is not a good idea at all. Querying on a random row implies scanning the whole dataset which will cost you a lot of money.
尽管如此,如果数据建模正确, query 方法可用于查找给定hash_key的最大range_key
Nonetheless, if your data is properly modeled, query method may be used to find the biggest range_key for a given hash_key
以下是操作步骤:
- 设置has_key
- 为range_key不设置过滤器
- 将结果计数限制为1
- 向后扫描索引
- Set the has_key
- Set no filter for the range_key
- limit the result count to 1
- scan the index backward
这篇关于在DynamoDB中选择每个组的前N条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!