amazon-dynamodbdatabase-partitioningcomposite-key

For partition key queries on composite keys, what is the cost?


Let's say we have a composite key : name (partition key) and timestamp (sort key)

If I query name with the latest timestamp, would I bear the cost of all entries inside the partition, or only the latest entry?


Solution

  • If you do a Query with Limit=1 and ScanIndexForward=False then you will get the latest value and only pay to read 1 item.

    aws dynamodb query \
    --table-name my-table \
    --key-condition-expression "#pk = :pk" \
    --expression-attribute-values '{":pk":{"S":"leeroy"}' \
    --expression-attribute-names '{"#pk":"name"}' \
    --no-scan-index-forward \
    --limit 1
    

    Likewise, if you know the name and timestamp you can do a GetItem and only pay to read 1 item.

    aws dynamodb get-item \
    --table-name my-table \
    --key '{"name": {"S": "leeroy"},"timestamp": {"S": "2023-01-01T20:00:000"}}'