Reputation: 349
Let's take the best practices for sort keys official documentation of DynamoDb as an example: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-sort-keys.html
Imagine we have a table like the documentation mentions, where our sort key is a composite that looks like:
[country]#[region]#[city]#[neighborhood]
For example, something like this:
Partition Key (Employee Name) | Sort Key | Other columns... |
---|---|---|
Antonio | Spain#Madrid#Getafe#Whatever | ... |
Maria | Spain#Andalucia#Sevilla#Whatever2 | ... |
Mike | Spain#Madrid#Alcorcon#Whatever | ... |
And I'd like to get all the records from a specific country + region, so we have a partial sort key:
[country]#[region]
like Spain#Madrid
to get Antonio
and Mike
.
I know it's not possible to query by sort key directly, so I created a GSI with the inverted index (like mentioned here https://stackoverflow.com/a/64141405)
Partition Key | Sort Key | Other columns... |
---|---|---|
Spain#Madrid#Getafe#Whatever | Antonio | ... |
Spain#Andalucia#Sevilla#Whatever2 | Maria | ... |
Spain#Madrid#Alcorcon#Whatever | Mike | ... |
But it still looks like it's not possible to query using the begins_with
operator.
var request = new QueryRequest
{
IndexName = "GSI_Name",
KeyConditionExpression = "begins_with(SortKey, :v_SortKey)",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{":v_SortKey", new AttributeValue { S = sortKey }},
},
};
My question is: is there any way to achieve this without using the Scan
operation which is not ideal? Or any suggestion to change my table definition to achieve this? I've been trying to think of ways of restructuring the table to accomplish this behavior, but I'm not fluent enough with DynamoDB.
Upvotes: 1
Views: 895
Reputation: 7152
Use the country as the PK and the rest as the SK. That spreads the data nicely across partitions while also enabling your access pattern.
Upvotes: 1