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
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.
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 .
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.)
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.
By mentioning only the hash key as attributes to get, you could have better performance and don't waste your throughput.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With