I have a Books table and I want to fetch 5 most expensive books! like we do in MySQL: SELECT * FROM (SELECT * FROM Books ORDER BY Price DESC) Books LIMIT 5
In general, DynamoDB is not designed to sort the entire table. Although you can Scan
the entire table, those will be presented in a seemingly-random order and not ordered by any familiar key. The DynamoDB documentation suggests why this is the case:
DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same partition key value are stored together, in sorted order by sort key value.
Sadly, the Scan
operation's documentation doesn't explain the implication of this on the ordering of the scan's results. Basically, a Scan
does not have any way of globally sorting all the partitions of the database (which may number in billions and be spread between many different servers) and each server only sees a subset of the partitions, sorted by this pseudo-random hash function ordering, so the ordering you see in a scan is influenced by this.
What DynamoDB does allow is you is to use an additional sort key (in addition to the partition key). A sort key will allow you to retrieve all the items that share the same partition key ordered by the chosen sort key. But it's still not all the items in the database - just the items in one partitions.
Of course, you can "cheat" by placing all your items in a single partition, and then you get them sorted. This will work well for a small database, but will not scale to huge amounts of data. DynamoDB officially limits the performance of a single partition to 3,000 RCU and 1,000 WCU (reads and writes per second, basically), so a scalable database needs to have many partitions - not just one.