manyu
manyu

Reputation: 291

Order in which data is stored for same GSI values in a DynamoDB table which does not have a range key over GSI

I quote the following from AWS documentation

Items with the same partition key value are stored in sorted order by sort key. If the sort key data type is Number, the results are stored in numeric order. For type String, the results are stored in order of ASCII character code values. For type Binary, DynamoDB treats each byte of the binary data as unsigned.

Suppose a table have a Global Secondary Index (without related range key) what will be the order in which data for same GSI value will be stored?

Does DynamoDB maintains some order based on some attribute while storing data in table (having just the partition key and no range key defined over the specific partition key) which have same value of partition key (this will occur only for GSI) just the Partition Key?

for e.g. In the following table will there be any order in which 3 items with GameTitle "GalaxyInvaders" would be stored in the Table?

(GSI)                           (HashKey)
——————————————————————————————————————————
GameTitle           | Score    | UserId
——————————————————————————————————————————
AlienAdventure      | 23       | 101
AttackShip          | 45       | 102
GalaxyInvaders      | 34       | 104
GalaxyInvaders      | 45       | 106
GalaxyInvaders      | 56       | 107

I ask this because I am making query with lastEvaluatedKey over a GSI (no range key is present for this GSI, table has a separate range and hash key), I want to know if I can go in both the direction i.e. get the next page of results and get the previous page of results with use of ScanIndexForward?

If some order is maintained then it seems that I can, though I could not find supportive documentation. From the queries that I have made till now supports that I can go in both the directions.

Upvotes: 2

Views: 2016

Answers (1)

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5659

In absence of a sort-key, DynamoDB will store data of one partition-key in some order. What that order will be, you can't and shouldn't assume.

What I mean is that, all rows with GameTitle equal to 'GalaxyInvaders' will be in some order, but you won't know what exactly that order is. So, you can't assume that rows will be sorted per Score or UserId. Such an assumption can only be made for sort-keys.

As for your actual question on Scan: yes, you can go both ways. If, you got rows a, b and c with ScanIndexForward : true, you can assume that you'll get back c, b and a with ScanIndexForward : false.

Upvotes: 1

Related Questions