Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB GSI BatchGetItem

Is it possible to retrieve rows from the dynamodb Global secondary index using batchgetitem api? If my aim is to retrieve data from the main table based on some non-key attribute also , but data should be retrieved in the batch of 100 items - is the GSI index won't fit here?

Also is BatchItemGet API available for Query? Say a table has the primary key and sort key and same primary key can have multiple sort keys can I retrieve multiple primary keys using batchItemGet with just primary key only or it won't fir here?

like image 539
user1846749 Avatar asked Jul 30 '17 16:07

user1846749


People also ask

Can we Query by GSI in DynamoDB?

You query the global secondary index through the DynamoDB API by using query and providing the index name.

Can we have multiple GSI in DynamoDB?

Some applications might need to perform many kinds of queries, using a variety of different attributes as query criteria. To support these requirements, you can create one or more global secondary indexes and issue Query requests against these indexes in Amazon DynamoDB.

How many GSI can you have DynamoDB?

Each table in DynamoDB can have up to 20 global secondary indexes (default quota) and 5 local secondary indexes. For more information about the differences between global secondary indexes and local secondary indexes, see Improving data access with secondary indexes.


2 Answers

There is no way to specify the index name in the BatchGetItem API operation according to the docs. That means using BatchGetItem (and GetItem for that matter) on a secondary index isn't possible. Both of these operate on the primary index.

If you want to retrieve data from a secondary index, you need to use Query or Scan. Both support the IndexName attribute according to the documentation. When using Query you have to specify the partition key and can optionally filter based on the sort key. If you don't filter on the sort key, you will get all items with the partition key, which should take care of your second requirement.

To retrieve data from a secondary index based on different partition keys, you'd need to issue multiple Query operations for the separate values of these keys, there is no batching here.

like image 57
Maurice Avatar answered Sep 20 '22 06:09

Maurice


You can use PartiQL with WHERE IN clause for that:

SELECT * FROM Orders WHERE OrderID IN [100, 300, 234]

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.select.html

like image 24
Anatoly Rugalev Avatar answered Sep 21 '22 06:09

Anatoly Rugalev