'Dynamo DB - Find the most recent entry for each primary key

I have a dynamoDB table where I have a primary key of "email" and a sort key of "timestamp" For audit purposes I dont have any editing or deletion, only insert. However what I now want is a query to select all users (emails) but only the most recent version of that primary key. Is this possible? I have tried to loop over every email and run a query to select based on primary key and limiting to 1 however my lambda quickly timed out and failed.



Solution 1:[1]

You need to issue n-many queries for n-many PKs, given your data model.

You can create a GSI and have a new attribute Latest as its PK and the ID as the SK and do a query against that and retrieve everything with one Query.

Make it a sparse index so you only populate Latest with a value if it is the latest.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hunterhacker