Anshul Singhal
Anshul Singhal

Reputation: 3

DynamoDB get count of items in last using secondary index

I have a table in DynamoDB that has columns(1. id, 2. item, 3. entry time), where id is the primary key. I need the count of all the items of one kind (there are two type of items let it be A and B, and I need the count of A) that are entered into the table within the last 24 hours. Is there any way I can do that using secondary index? I can't find much information related to this.

Upvotes: 0

Views: 987

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

Global secondary indexes will help you aggregate items of the same type, but they will not give you a count of these items. While this may be useful in your application, it doesn't exactly address your access pattern.

Unlike its SQL counterparts, DynamoDB does not provide aggregation operations out-of-the-box. If you want totals, minimums, maximums, averages, etc., you're going to need to maintain those aggregations yourself.

A common pattern for implementing this feature is to use DynamoDB streams to maintain an aggregation in your table.

When you enable DynamoDB streams on your table, you configure a lambda to execute every time an operation is performed on your table (e.g. insert, delete, update, etc). When this lambda is notified of an event you care about (e.g. a new item of Type A was inserted/deleted), you would increment/decrement the total count of items of Type A in your table. If you follow this pattern, supporting your access pattern can be done with a very fast getItem operation.

Upvotes: 1

Related Questions