Reputation: 910
Given my DynamoDB has a column of 'BlockNumber', how do I write the Java QuerySpec to find the MAX block number in the DB? (It is configured as a GSI.)
Upvotes: 1
Views: 1398
Reputation: 8885
If the data is immutable the best option for this is to have a separate record that holds aggregate values. Whenever you add an item that may change the max value you would update the aggregate record. The best approach to this is to use DynamoDB streams to perform the updates to the aggregate record. Using Global Secondary Indexes for Materialized Aggregation Queries
Upvotes: 0
Reputation: 78803
Typically, your GSI would have a partition key and a sort key, just like a regular DynamoDB table. You would issue a query against a known partition key and set ScanIndexForward=false and Limit=1, so it would return one item only, and it would be the item with a matching partition key and the maximum value of the sort key. When ScanIndexForward is false, DynamoDB reads the items in reverse order by sort key value.
Upvotes: 3