'How does DynamoDB Select/Filter records based on the range key?
This is based on the documentation provided by AWS - DynamoDB. When a query is issued, the following steps happen in order:
- DynamoDB queries based on the primary key
- It applies the filter
- If there are any items left: return the results
In the example below, I have a primary key and sort key (range key). I understand the range key will be combined with primary key to form a composite primary key. results will be selected based on the primary+sort key in the step 1?
Here is the YAML syntax for DynamoDB table creation with partition and sort keys.
AWSTemplateFormatVersion: "2010-09-09"
Resources:
usersTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
-
AttributeName: "Id"
AttributeType: "S"
-
AttributeName: "rId"
AttributeType: "S"
KeySchema:
-
AttributeName: "Id"
KeyType: "HASH"
-
AttributeName: "rId"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
TableName: "mytable"
Solution 1:[1]
There are some misconceptions here in terms of vocabulary.
A Primary Key identifies items uniquely in DynamoDB. There are two kinds of primary keys:
- "Regular" Primary Key, which means each item is identified only by its Partition Key (HASH_KEY in the API)
- Composite Primary Key, which means a combination of a Partition identifies each item- and Sort Key (RANGE_KEY in the API)
The table definition decides which key will be used. In your case, it's a composite primary key.
Items in DynamoDB are saved in a partition. The partition key decides on which partition the item is stored. The sort key determines in which order items that share the same partition key are stored.
When you select items using the Query API, you have to specify the partition key and can optionally filter based on the sort key. Based on the partition key, DynamoDB knows which storage partition to talk to and can then optionally filter items based on the sort key. Only after this is done, the filter expression is applied and you can filter based on the results of the query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maurice |
