2

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?

rboy
  • 2,018
  • 1
  • 23
  • 35
  • If what I found is correct, there's no way to limit the number of objects returned when using `context.Query` - https://benfoster.io/blog/20200615-querying-a-dynamodb-partition-with-dotnet/ – rboy Sep 10 '20 at 01:11
  • I cannot answer your question, but I am suspicious of your assumption. FIRST_VALUE() is a SQL function. I would expect Microsoft to add that as part of your query and not return the results from your DB into C# space. I am not saying that I "know" that it does this, only that it is possible and Query is generally pretty smart about such things. – Frank Merrow Sep 10 '20 at 03:51

1 Answers1

2

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.

Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71
  • Any chance you could share the best way to run this query using the least amount of code with DynamoDbClient? – rboy Sep 10 '20 at 17:47
  • on a related note, do you know if `DynamoDbContext` allows for strong consistent reads? – rboy Sep 16 '20 at 01:37