1

I want to check if a specific key has a specific value in a dynamodb table with/without retrieving the entire item. Is there a way to do this in Python using boto3?

Note: I am looking to match a sort key with its value and check if that specific key value pair exists in the table,

4
  • Add caching layer, otherwise not sure how to retrieve something from dynamo without retrieving it. Am I missing something? Are you trying to not consume the minimum 2kB per request or something? Commented Mar 11, 2021 at 23:43
  • I am ok to retrieve the item but can i match on the sort key? Commented Mar 11, 2021 at 23:52
  • Are you trying to search for an item by specifying only the sort key? Commented Mar 12, 2021 at 0:34
  • You're you going to need the partition key, as well or you'll being a scan. Another option if this is going to be a frequent access pattern is to make a GSI with the base tables SK as the GSI's PK. Commented Mar 12, 2021 at 6:32

2 Answers 2

1

It sounds like you want to fetch an item by it's sort key alone. While this is possible with the scan operation, it's not ideal.

DynamoDB gives us three ways to fetch data: getItem, query and scan.

The getItem operation allows you to fetch a single using it's primary key. The query operation can fetch multiple items within the same partition, but requires you to specify the partition key (and optionally the sort key). The scan operation lets you fetch items by specifying any attribute.

Therefore, if you want to fetch data form DynamoDB without using the full primary key or partition key, you can use the scan operation. However, be careful when using scan. From the docs:

The Scan operation returns one or more items and item attributes by accessing every item in a table or a secondary index.

The scan operation can be horribly inefficient if not used carefully. If you find yourself using scans frequently in your application or in a highly trafficked area of your app, you probably want to reorganize your data model.

Sign up to request clarification or add additional context in comments.

1 Comment

To add to the last paragraph. Even if you use SCANs infrequently, but they are used in parts of the app's user traffic pattern that are hit frequently, the experience will be sub-optimal and expensive.
0

What Seth said is 100% accurate, however, if you can add a GSI you can use the query option on the GSI. You could create a GSI that is just the value of the sort key, allowing you to query for records that match that sort key. You can even use the same field, and if you don't need any of the data you can just project the keys, keeping the cost relatively low.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.