按日期查询DynamoDB按日、DynamoDB

2023-09-11 10:26:48 作者:小高傲丶

我是从一个关系型数据库背景的,并试图与亚马逊DynamoDB工作

我有一个表散列关键字数据ID和一系列CreatedAt,并在这一堆物品。

我试图让所有的特定日期之后创建和按日期排序的项目。这是pretty的简单的关系数据库。

在DynamoDB是最接近我能找到的是一个查询和使用范围键大于过滤器。唯一的问题是,要执行一个查询,我需要一个散列键这违背了目的。

那么,我做错了什么?是我的表模式错了,不应该散列键是唯一的?或者有另一种方式来查询?

解决方案

更​​新答:

DynamoDB允许指定辅助索引在这样的查询,以帮助。第二索引可以是全局的,这意味着索引横跨混杂键的整个表,或本地的含义,该指数将每个散列键分区中存在,因此要求散列键也可以使查询时指定。

有关使用情况下,在这个问题上,你可能需要使用一个全球性的辅助索引上的CreatedAt字段。

更多关于DynamoDB二级指标看到sceondary索引文件

原来的答案:

DynamoDB不允许只在范围键索引的查找。散列键是必需的,使得该服务知道看在哪个分区查找数据。

从MySQL到AWS DynamoDB数据库的迁移实践

您当然可以执行扫描操作的日期值过滤,但是这需要全表扫描,所以它是不理想的。

如果你需要通过在多个主键的时间来进行记录的一个索引查找,DynamoDB可能不是理想的服务供您使用,或者您可能需要使用一个单独的表(在DynamoDB或关系店)存储项目的元数据,您可以执行对索引的查找。

I'm coming from a relational database background and trying to work with amazon's DynamoDB

I have a table with a hash key "DataID" and a range "CreatedAt" and a bunch of items in it.

I'm trying to get all the items that were created after a specific date and sorted by date. Which is pretty straightforward in a relational database.

In DynamoDB the closest thing i could find is a query and using the range key greater than filter. The only issue is that to perform a query i need a hash key which defeats the purpose.

So what am I doing wrong? Is my table schema wrong, shouldn't the hash key be unique? or is there another way to query?

解决方案

Updated Answer:

DynamoDB allows for specification of secondary indexes to aid in this sort of query. Secondary indexes can either be global, meaning that the index spans the whole table across hash keys, or local meaning that the index would exist within each hash key partition, thus requiring the hash key to also be specified when making the query.

For the use case in this question, you would want to use a global secondary index on the "CreatedAt" field.

For more on DynamoDB secondary indexes see the sceondary index documentation

Original Answer:

DynamoDB does not allow indexed lookups on the range key only. The hash key is required such that the service knows which partition to look in to find the data.

You can of course perform a scan operation to filter by the date value, however this would require a full table scan, so it is not ideal.

If you need to perform an indexed lookup of records by time across multiple primary keys, DynamoDB might not be the ideal service for you to use, or you might need to utilize a separate table (either in DynamoDB or a relational store) to store item metadata that you can perform an indexed lookup against.