Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Dynamodb Sort the query results when there is no range key avaialble?

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?

like image 427
Asanga Dewaguru Avatar asked Aug 09 '16 07:08

Asanga Dewaguru


People also ask

Is Range key mandatory in DynamoDB?

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.

Can you query DynamoDB without partition key?

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.

Can we query DynamoDB table without primary key?

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.

How do I sort DynamoDB query results?

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.


1 Answers

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:

  • scan all data and order on the client
  • create a global secondary index with a common value for partition key for all items and query that
  • design a complementary storage layer to provide ordering

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.

like image 139
Mike Dinescu Avatar answered Nov 08 '22 20:11

Mike Dinescu