Reputation: 3607
If you perform a "get_item" operation on a dynamodb table, you must provide a partition (primary) key.
You are not allowed to perform "get_item" operation on a GSI (global secondary index) even through it acts quite the same as the partition (primary) key.
To work with GSI you rather need to "query" the databse.
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
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
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