I'm using dynamoDb to store some additional info but I have some troubles sorting my data.
I have the following create syntax where I have a storeId
and number
keys. I set the number
as sort key, but the problem is that the data isn't sorted at all.
$response = $dynamoDb->createTable([
'TableName' => 'foo',
'KeySchema' => [
[
'AttributeName' => 'storeId',
'KeyType' => 'HASH' //Partition key
],
[
'AttributeName' => 'number',
'KeyType' => 'RANGE' // sort Key
]
],
'AttributeDefinitions' => [
[
'AttributeName' => 'storeId',
'AttributeType' => 'N'
],
[
'AttributeName' => 'number',
'AttributeType' => 'N'
]
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 20,
'WriteCapacityUnits' => 20
]
]);
My scan params:
$scanParams = [
'TableName' => 'foo',
'ProjectionExpression' => '#storeId, #number',
'FilterExpression' => '#number >= :n',
'ExpressionAttributeNames'=> [ '#storeId' => 'storeId', '#number' => 'number'],
'ExpressionAttributeValues' => [
':n' => ['N' => $number]
]
];
The result of my scan:
StoreId number
68001 80000
25000 37000
463501 527000
4800001 5300000
360001 400000
2000001 2600000
As you can see, the data isn't sorted on the number property.
Just read the first paragraph below. The sort key is used to store all the items of the same partition key value physically close together and sorted in ascending order by default (i.e. important point is data is not sorted across the partition key). In other words, the data is sorted in ascending order by default for the same partition key.
Example:-
Partition Key Sort Key
p1, s1
p1, s2
p1, s3
p2, s1
p2, s2
If the table has a composite primary key (partition key and sort key), DynamoDB calculates the hash value of the partition key in the same way as described in Data Distribution: Partition Key—but it stores all of the items with the same partition key value physically close together, ordered by sort key value.
To write an item to the table, DynamoDB calculates the hash value of the partition key to determine which partition should contain the item. In that partition, there could be several items with the same partition key value, so DynamoDB stores the item among the others with the same partition key, in ascending order by sort key.
To read an item from the table, you must specify its partition key value and sort key value. DynamoDB calculates the partition key's hash value, yielding the partition in which the item can be found.
In Query API, there is a parameter to get the result in ascending or descending order.
ScanIndexForward: true || false
ScanIndexForward — (Boolean)
Specifies the order for index traversal: If true (default), the traversal is performed in ascending order; if false, the traversal is performed in descending order.
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.
If ScanIndexForward is true, DynamoDB returns the results in the order in which they are stored (by sort key value). This is the default behavior. If ScanIndexForward is false, DynamoDB reads the results in reverse order by sort key value, and then returns the results to the client.