简体   繁体   English

如何在条件之间应用带有分区键的 DynamoDB 表?

[英]How can I DynamoDB Table with Partition Key on apply between condition?

sql query like this: sql 这样查询:

select * from table where Date BETWEEN '2020-07-01' AND '2020-08-18'
           OR
select * from table where Date >= '2020-07-01' AND Date <= '2020-08-18'

How can i perform above condition in DynamoDB with partition key.我如何使用分区键在 DynamoDB 中执行上述条件。

If you're using the partition key (and not a sort key) then you have 2 choices.如果您使用的是分区键(而不是排序键),那么您有 2 个选择。

You either would use a Scan which supports a ScanFilter of BETWEEN , or you would need to perform a query against each partition key individually.您要么使用支持BETWEENScanFilter的 Scan,要么需要分别针对每个分区键执行查询。

AWS have published an example of using Scans in PHP within their documentation. AWS 在其文档中发布了一个使用扫描的示例PHP。

Generally you would try to avoid using Scans as it is very inefficient from a resource point of view and will consume large amounts of RCU as it tries to retrieve the result, whereas a Query is more efficient.通常,您会尽量避免使用 Scans,因为从资源的角度来看,它的效率非常低,并且在尝试检索结果时会消耗大量的RCU ,而 Query 效率更高。

This is because a Scan operation is performed across all partitions with any filters being applied after the data has been retrieved, whereas a Query operation is performed on a single partition with filters applied before data is retrieved.这是因为扫描操作是在所有分区上执行的,并且在检索数据应用了任何过滤器,而查询操作是在单个分区上执行的,并且在检索数据之前应用了过滤器。

If this is an issue for you, you could look at remodelling the DynamoDB table to allow the usage of a better partition key and use the data range as a sort key for the table.如果这对您来说是个问题,您可以考虑重构 DynamoDB 表以允许使用更好的分区键并将数据范围用作表的排序键。 Alternatively you could create a GSI on your table to have a partition key shared by all the data, with the date range used as a sort key.或者,您可以在表上创建一个GSI ,让所有数据共享一个分区键,并将日期范围用作排序键。 This would allow you to filter the sort key using a query condition.这将允许您使用查询条件过滤排序键。

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

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