I've a Dynamodb table that contains only a hash key (partition key). There is no range key (sort key) for this table. Hash key is a numeric value (eg:- 1,2,3... N) When I query or Scan this table, which order they send the results in? If there is a range key, it obviously sorts the results in ascending order based on the range key value. But in this case without a RANGE KEY, how does it behave?
There are two types of indexes in DynamoDB, a Local Secondary Index (LSI) and a Global Secondary Index (GSI). In an LSI, a range key is mandatory, while for a GSI you can have either a hash key or a hash+range key. GSIs span multiple partitions and are placed in separate tables.
The Query operation in Amazon DynamoDB finds items based on primary key values. You must provide the name of the partition key attribute and a single value for that attribute.
Hash key in DynamoDB The primary reason for that complexity is that you cannot query DynamoDB without the hash key. So, it's not allowed to query the entire database. That means you cannot do what you would call a full table scan in other databases.
You can use the sort-key and apply the ScanIndexForward parameter in a query to sort in either ascending or descending order. Here I limit items returned to 1. Save this answer.
DynamoDB does not offer any ordering guarantees (implicit or explicit) for scanning items from a table. The items are more or less returned in the order that the internal hashing function maps items to partitions of the table but there are no implicit or explicit ordering guarantees.
Even with a sort key, the ordering is implied only within the items of the same partition key. So even if you had a range key on the table, that would only allow you to query items in order for a specific hash key at a time.
If you need to order all items in a table then you have a few options:
It's important to stress that the first two options above are only good for relatively small tables. Once you reach about 10GB in the table having a GSI with a common key will act as a significant bottle-neck and of course scanning and order the whole table becomes problematic as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With