Reputation: 2165
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?
Upvotes: 2
Views: 1627
Reputation: 13973
No, this isn't possible. The data model mode is very dumb: it provides a convenient interface to perform common operations without needing to write any boilerplate, at the cost of performance and control.
You'll need to use the low-level DynamoDbClient. For an application that needs control over its requests, you'll likely want to drop the DynamoDbContext
, and instead create your own wrapper around the client that exposes whichever request properties you need.
Keep in mind that the DynamoDbContext
is still useful for its FromDocument
and ToDocument
methods, which are a huge help in (de)serializing objects to/from the attribute dictionaries found in the client's request/responses.
Upvotes: 2