How to Query Multiple Hash Keys in Dynamodb

Is there a way to query multiple hash keys in DynamoDB?

No, as of today there is no way to send multiple queries in the same request. If you're concerned about latency, you could make multiple requests simultaneously in different threads. This would require the same amount of network bandwidth as a "dual query" would if Dynamo offered it (assuming you're making 2, not hundreds).

Multiple attributes as Hash Key in Dynamo DB

You can only have one field as each of the two keys, so one HASH and one RANGE key. If you need multiple fields you'll need to create a composite key. For example, create an attribute named a1a2 for the HASH and a3a4 for the range.

DynamoDB - fetching items by multiple keys

You need to make one query per unique id. Each of these queries should include a key condition expression that has equality on the id partition key and range of values on the date sort key, like this:

#id = :id AND #date BETWEEN :startdate AND :enddate

Don't use scan for this. As your table grows, performance will decline.

AWS DynamoDB. Querying all hashes IN array

Query doesn't provide what you want. As per the documentation here:

KeyConditionExpression: The condition must perform an equality test on a single partition key value. The condition can also perform one of several comparison tests on a single sort key value. Query can use KeyConditionExpression to retrieve one item with a given partition key value and sort key value, or several items that have the same partition key value but different sort key values.

BatchGetItem is the only option that you have.

How to query and order on two separate sort keys in DynamoDB?

As written, no this isn't possible.

within a specific birthday range

implies sk_birthday between :start and :end

sorted by lastActive

implies lastActive as a sort key.

which are mutually exclusive...I can't devise a sort key that would be able to contain both values in a usable format.

You could have a Global Secondary Index with a hash key of group-id and lastActive as a sort key, then filter on birthday. But, that only affects the data returned, it doesn't affect the data read nor the cost to read that data. Additionally, since DDB only reads 1MB of data at a time, you'd have to call it repeatedly in a loop if it's possibly a given group has more than 1MB worth of members.

Also, when your index has a different partition (hash) key than your table, that is a global secondary index (GSI). If your index has the same partition key but a different sort key than the table, that can be done with a local secondary index (LSI)

However for any given query, you can only use the table or a given index. You can't use multiple indexes at the same time

Now having said all that, what exactly to you mean by "specific birthday range" If the range in question is a defined period, by month, by week. Perhaps you could have a GSI where the hash key is "group-id#birthday-period" and sort key is lastActive

So for instance, "give me GROUPA birthdays for next month"

Query(hs = "GROUPA#NOVEMBER")

But if you wanted November and December, you'd have to make two queries and combine & sort the results yourself.

Effective and efficient use of DDB means avoiding Scan() and avoiding the use of filterExpressions that you know will throw away lots of the data read.

dynamodb query by hashkey+multiple range keys

The DynamoDBMapper has an API for retrieving multiple items from multiple tables based on the primary key, which is documented here.

The method you're looking for is

public java.util.Map<java.lang.String,java.util.List<java.lang.Object>> 
batchLoad(java.util.Map<java.lang.Class<?>,java.util.List<KeyPair>> itemsToGet)

Where the KeyPair will be used to list your Hash/Range keys



Related Topics



Leave a reply



Submit