Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 499
Chen Harel Avatar asked Mar 20 '12 18:03

Chen Harel


People also ask

How can I make DynamoDB Query faster?

You can increase your DynamoDB throughput by several times, by parallelizing reads/writes over multiple partitions. Use DynamoDB as an attribute store rather than as a document store. This will not only reduce the read/write costs but also improve the performance of your operations considerably.

Is DynamoDB Query fast?

You might think that DynamoDB Query operation is fast, but it has its own limits. As per documentation: A single Query operation will read up to a maximum of 1 MB of data and then apply any filtering to the results using FilterExpression .

Which is faster scan or Query in DynamoDB?

For faster response times, design your tables and indexes so that your applications can use Query instead of Scan . (For tables, you can also consider using the GetItem and BatchGetItem APIs.)


2 Answers

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.

like image 121
Sony Kadavan Avatar answered Oct 08 '22 22:10

Sony Kadavan


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

like image 37
Ashwin Patti Avatar answered Oct 08 '22 22:10

Ashwin Patti