Laimonas Sutkus
Laimonas Sutkus

Reputation: 3607

DynamoDB "query" vs "get_item" operations on a partition key or a GSI

So, my question is: is "query" operation with "equals" operator for a GSI is as efficient as "get_item" for the partition key?

Upvotes: 0

Views: 2808

Answers (2)

Mike Dinescu
Mike Dinescu

Reputation: 55730

The reason for requiring a query operation when reading data from a GSI is that unlike the table itself, a GSI may contain multiple items for the same key. So, whereas for the table itself you are guaranteed, and forced, to only ever have one item per partition + sort key, the GSI could end up with multiple items mapping to the same partition + sort key in the GSI.

I'm not going to comment on the performance of querying a GSI compared to a get item on the main table other than to say that you should run your own benchmark if you're concerned about performance, though in most cases the difference should be negligible.

Upvotes: 2

Chris Williams
Chris Williams

Reputation: 35188

I believe GetItem is ever so slightly better performance as it will only ever be one item from a single hash key (Partition key and optionally the sort key).

Query is efficient as it will only ever retrieve from a single partition, however, can return multiple items dependent on the operator used for the sort key.

Upvotes: 1

Related Questions