英文:
DynamoDB two Global Secondary Indexes with the same hash key order by a specific sort key
问题
当我使用筛选条件 studentGroup = 1
扫描表时,DynamoDB 返回的结果按照 mathScore
还是 scienceScore
的顺序排列?
是否有办法指定使用哪个属性(或哪个GSI)进行排序?
如果不可行,那么在设计GSI以便应用程序可以选择属性进行排序方面的最佳实践是什么?
我尝试过从DynamoDB管理控制台进行操作,但似乎无法指定用于扫描的索引。
英文:
I have a DynamoDB table with two GSI:
GSI 1
- hash key:
studentGroup
- sort key:
mathScore
GSI 2
- hash key:
studentGroup
- sort key:
scienceScore
When I scan the table with filter studentGroup = 1
, does DynamoDB return the results in the order of mathScore
or scienceScore
?
Is there any way to specify which attribute (or which GSI) to be used for sorting?
If not possible, what's the best practice to design GSI to sort by an attribute that the application side can choose?
I tried from the DynamoDB management console, but it seems no way to specify which index to be used for scanning.
答案1
得分: 1
在DynamoDB中,当执行Scan
操作时,不会跨键维护顺序,而只会在每个项目集合中维护顺序(共享相同键的项目)。
如果您想要按照mathScore对studentGroup=1进行排序,那么请从GSI1中读取。
如果您想要按照scienceScore进行排序,那么请从GSI2中读取。
在发出请求时,您必须指定要使用的索引。
aws dynamodb query \
--table-name <TableName> \
--index-name <GSI1或GSI2> \
--key-condition-expression <条件>
英文:
In DynamoDB when you do a Scan
there is no order maintained across keys, but only per item collection (items which share the same key).
If you want a result where studentGroup=1 ordered by mathScore then you read from GSI1.
If you want it ordered by scienceScore then read from GSI2.
You must specify which index when you make the request.
aws dynamodb query \
--table-name <TableName> \
--index-name <GSI1 or GSI2> \
--key-condition-expression <>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论