I'm trying to limit then number of items returned by DynamoDB while querying a table. I need only the first matching result.
The goal is reduce the number of read units consumed and in an effort to keep the code readable I'm using context.Query
to query a secondary local index and get the first matching result.
This is the current query:
context.Query<TableEntry>(matchId, new DynamoDBOperationConfig { IndexName = "SecondaryId-index" })
This will return all items who's SecondaryId-index
matches matchId
, however I only want the FIRST item it matches.
So I tried this:
context.Query<TableEntry>(matchId, new DynamoDBOperationConfig { IndexName = "SecondaryId-index" }).First()
As I understand context.Query
(even though it's lazy) will still execute the entire query and read atleast the first page (when enumerating using .First()
) and the page will contain more than 1 item, hence consuming more read capacity units than required.
I can't find any documentation on how to limit the query to the FIRST matching item only using context.Query
in .NET.
Is that even possible? If not, what the next best alternative while keeping the code minimal/clean?