Dhiraj Maurya
Dhiraj Maurya

Reputation: 41

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

sql query like this:

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.

Upvotes: 1

Views: 206

Answers (1)

Chris Williams
Chris Williams

Reputation: 35176

If you're using the partition key (and not a sort key) then you have 2 choices.

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.

AWS have published an example of using Scans in PHP within their documentation.

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.

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. 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. This would allow you to filter the sort key using a query condition.

Upvotes: 1

Related Questions