Search code examples
amazon-web-servicesamazon-dynamodbclouddynamodb-queriesamazon-dynamodb-index

Dynamo Db SDK for .NET - what is the preferable way to query data by key?


I see that IDynamoDBContext from official AWS SDK for DynamoDb has different methods to query stored data:

    Task<T> LoadAsync<T>(object hashKey, object rangeKey, CancellationToken cancellationToken = default (CancellationToken));


    AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig = null);

QueryFilter passed to QueryOperationConfig used by FromQueryAsync() has a method for adding query conditions with the first parameter named keyAttributeName.

 public class QueryFilter : Filter
 {

   public void AddCondition(
         string keyAttributeName,
         QueryOperator op,
         params DynamoDBEntry[] values)
...

Does this mean that querying DynamoDb with FromQueryAsync() set with the correct type of condition (for key attributes) performs as fast as querying data by primary key calling LoadAsync(hashKey)?

In other words, are calls to method A and method B similar in performance?

        protected Task<T> A<T>(string hashKey)
        {
            return _dynamoDbContext.LoadAsync<T>(hashKey,
                _consistentReadConfig,
                CancellationToken.None);
        }

        protected async Task<T> B<T>(string hashKey)
        {
            var queryFilter = new QueryFilter();
            queryFilter.AddCondition("HashKeyProperty", QueryOperator.Equal, hashKey); // adding condition for hash key equality

            var queryOperationConfig = new QueryOperationConfig
            {
                Filter = queryFilter
            };

            var queryOperation = _dynamoDbContext.FromQueryAsync<T>(
                queryOperationConfig,
                _consistentReadConfig);

            var results = await queryOperation.GetNextSetAsync();

            return results.SingleOrDefault();
        }


Solution

  • I would expect them to be...though I believe the DDB SLA only applies to GetItem()

    But the point of Query() is to return more more than a single record. Query() is only useful if your table has a composite primary key (hash+sort). You query() based upon a given (EQ) hash key and possibly some function (LT, GT, Starts with) on the sort key.

    GetItem() requires the full primary key, and can only return 1 record.

    Point I'm trying to make, your app should know if it has the PK and only needs one record, or if you just have a partial key and expect a list of records back.