If I'm retrieving a single item from my table based on the indexed hash key, is there a performance difference between query()
or getItem()
?
4 Answers
getItem
will be faster
getItem
retrieve via hash and range key is a 1:1 fit, the time it takes (hence performance) to retrieve it is limited by the hash and sharding internally.
Query results in a search on "all" range keys. It adds computational work, thus considered slower.
Edit: Just for quick comparison adding the following picture from this blog entry.

- 534
- 1
- 7
- 20

- 9,684
- 5
- 44
- 58
-
What is a range key? Do you mean sort key? – Sebastian Nielsen Nov 08 '22 at 13:41
-
@SebastianNielsen back in the day (this answer is from 2012!) DynamoDB called it
. Then around the time the document api was released, the conversation shifted to – Chen Harel Nov 09 '22 at 15:29..
In Amazon's DynamoDB, your performances are guaranteed whatever the access method. (you pay for it).
There may be a couple a milliseconds differences on the DynamoDB servers themselves as suggested by Chen Harel but these are negligible because of the HTTP request RTT.
This said, it's a good practice to issue a GET
instead of QUERY
when you have enough informations to do so.

- 6,840
- 1
- 37
- 48
-
16Those couple of milliseconds can make a huge difference at scale, even in the context of an HTTP request. That 2-3ms additional latency to DynamoDB could mean 2-3ms time that a request remains open between your web server and your backend service, tying up resources. – Annabelle Jun 26 '15 at 00:04
-
6It's good people are thinking of performance. When we talk about optimizations of 2-3ms, there are not many webservices around the world that really need to think of this optimization. 99% of all webservices will never even notice this little delay. – infinity1975 Oct 01 '18 at 07:29
As suggested by aws employee in one of the discussion, I quote:
The latency of GetItem vs Query with limit=1 will be equivalent.

- 619
- 7
- 8
There is no performance difference between the two. The hash calculation in both the queries are done 1 by 1. The latter, i.e., get item is just provided as an analogy to the JPA repository/spring findOne/findById to make wiring in Spring Bean wiring/ Hibernate configs easier.

- 44
- 6