Chen Harel
Chen Harel

Reputation: 10052

Quickly query a table if it contains a key (DynamoDB and Java)

I have a table with a hash and range complex key.
I can query an item using GetItem from AWS SDK for Java. The GetItem returns null if it doesn't find the object, or the item as a Map<String, AttributeValue>.
I am looking for the fastest approach to check whether the object does exist
I was thinking maybe supplying a .withAttributesToGet such as:

GetItemResult result =  dbClient.getItem(new GetItemRequest().
    withTableName(TABLE_NAME).
        withKey(new Key(new AttributeValue().withS(hashKey),
                        new AttributeValue().withS(rangeKey))).
        withAttributesToGet(new ArrayList<String>()));
Map<String, AttributeValue> item = result.getItem();
return (item != null);

Another optimization is to not use the SDK JSON parser and parse the response myself to quickly check if the item has returned.

Thanks

Upvotes: 19

Views: 30765

Answers (2)

Sony Kadavan
Sony Kadavan

Reputation: 4052

I think there is negligible difference in speed between "getting" and checking if it exists. You can go ahead and use the GetItem itself. If the item is potentially too large, then limit the attributes being returned.

The bottle neck is in latency to reach the Dynaamo DB servers (REST API) and in fetching from the index. So Getting and checking will be similar speed. Ensure that your server issuing the call is in the same region as Dynamo DB - This has max impact on the speed.

Upvotes: 22

Ashwin Patti
Ashwin Patti

Reputation: 598

By mentioning only the hash key as attributes to get, you could have better performance and don't waste your throughput.

Upvotes: 7

Related Questions