Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetching top 10 records without using scan in DynamoDB

I have a DynamoDB table containing:
productID (PK), name, description, url, createTimestamp, <constant>

I’m trying to retrieve the latest 10 products by createTimestamp (unix timestamp).

In SQL, I would probably pull out the data like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select * from [table] order by createTimestamp desc limit 10;

Q: How can I achieve the same result using DynamoDB without using scan?

The table can be pretty large and data will be accessed often (e.g., whenever user access the e-commerce website) so using scan wouldn’t be optimal. I’m thinking of creating a GSI using a constant value as PK (because there isn’t any other attribute we could use to narrow the results) and sort key as createTimestamp but this is considered anti-pattern. Is there a better alternative?

>Solution :

That’s the way to go, with a GSI having a singular PK and the timestamps in the SK.

If your write rate will exceed 1,000 write units per second then you’ll want to shard the PK value to one of N many randomly chosen values to increase throughout to N,000 writes per second.

That means you’ll need to do N many Query calls to get your unified answer but each Query will be highly efficient and index optimized.

This is a common design pattern.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading