Fetch all the records from Dynamo DB table using Query (python boto3)

I have a table in Dynamo Db with 2 million records. The table has date (yyyy-mm-dd) as partition key and account I'd as sort key. Currently, we are using boto3.client.dynamodb.query to fetch the data based on KeyConditionExpression where X is date. I want to query the records based on just year? What should KeyConditionExpression and Expression Attribute Values be?

Sample row in Dynamo Db :

{"date": {"s" : "2022-04-29"},"account_id": {"s" :"6208378688923445"}}

Here date is partition key and account_id is sort key. I want to query all the records with year 2022.

Upvotes: 0

Views: 480

Answers (1)

Ross Williams
Ross Williams

Reputation: 632

A query can only operate on a single partition key. If you want to run this query, you need to add a GSI with the year as the partition key. This has additional write and storage costs. If you don’t need to run this query very often, it may be cheaper to just scan the table and filter in the application layer. If you have 2 million records and each record is 500 bytes, the cost of a scan is less than $0.02

Upvotes: 1

Related Questions