在 dynamodb 中查询一系列主键 [英] Query a range of primary keys in dynamodb

查看:20
本文介绍了在 dynamodb 中查询一系列主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保我做对了,
根据我目前阅读的内容,您无法查询 dynamodb 中的一系列主键,
例如,如果您的主键是数字,例如客户的电话号码,则无法获取主键大于 3010000000 或介于 3010000000 和 3020000000 之间的项目
说清楚,我不是在谈论范围键,我的问题是关于主键本身,

I want to make sure I get this right,
Based on what I've read so far, you can NOT query a range of primary keys in dynamodb,
like if you have a primary key which is number like the phone number of your customers, you can not get items with primary keys larger than 3010000000 or between 3010000000 and 3020000000
to make it clear, I am not talking about the range key, my questions is about the primary key itself,

所以如果这是真的,那么会有很多用例,比如日期之间的项目、在某个时间点之后注册的用户,以及...,这需要表扫描,

so if this is true, there are lots of use cases, like items between dates, users registered after some point, and... , that requiers either table scans,

这是正确的吗?

好的,想到的一种解决方案是只使用一个虚拟 hash_key 作为主键,然后插入真正的键(如上面的电话号码)作为范围键,这可行吗?

OK, one solution that comes to mind, would be to use only one dummy hash_key for primary key and insert the real key (like phone numbers above) as range keys, does this work?

推荐答案

是的,您无法使用 DynamoDb 获取 hash_key 的范围.但这并不意味着您会被自己的用例所困.

Yes, you can not get a range of hash_key with DynamoDb. But this does not mean you are stuck with your use case.

让我们以日期"用例为例,假设您正在构建一个日志应用程序.您每天可能会获得大量记录.

Let's take the 'dates' use case and say your are building a logging application. You are likely to get lots of records each day.

如果你使用天作为 hash_key,你可以把完整的时间戳作为 range_key.通过这种方式,您可以将查询拆分为多个块并获得所需的内容.

If you use the day as the hash_key, you can put the full timestamp as the range_key. This way, you can split your query into chunks and get what you want.

当然,要获得最佳结果,您需要充分了解查询的类型.例如,典型范围是多少?使用 DynamoDb 以及其他 key:value 存储,您在大多数时间建模数据时都会考虑到查询,这与 SQL 建模时只考虑数据不同.

Of course, to get the optimal results, you will need to know well the kind of queries. For example, what is the typical range ? With DynamoDb, as well as other key:value store, you most of the time model your data with query in mind, unlike SQL when you model with only data in mind.

当然,如果您的物品跨越更大/更短的范围,只需调整此系统即可.

Of course, if your items spans on larger/shorter range, just adapt this system.

关于所有都在同一个虚拟 hash_key 下"听起来是个糟糕的主意.对不起.我不是百分百确定它是如何工作的,但我知道 DynamoDB 会跨所谓的分区进行一些分片.我相信 1 hash_key <=>1 个分区.此外,如果仔细阅读文档,您会注意到预置吞吐量在分区之间平均分配,因此每个分区只分配了您所支付费用的一小部分.

Concerning the "all under the same dummy hash_key" sounds like a terrible idea. Sorry. I am not a hundred percent sure how it really works but I know DynamoDB does some sharding across so called partitions. I believe 1 hash_key <=> 1 partitions. Moreover, If read closely the documentation, you'll notice that the provisionned throughput is splited evenly between the partitions so that each partitions is only allocated a fraction of what you pay for.

这篇关于在 dynamodb 中查询一系列主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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