首页 文章

在DynamoDB中排序

提问于
浏览
0

使用 Scan/Query API时,有没有办法从 Dynamodb 中获取排序结果?我知道在 Query API中你可以按 RangekeyScanIndexForward 进行排序,如果值为真,则对结果进行排序,如果为false,则对降序进行排序;

但据我所知,你可以有一个范围键,那么如果我想根据不同的字段进行排序呢?

此外,如果我使用扫描,似乎没有选项来排序结果!

任何帮助表示赞赏!

1 回答

  • 0

    对于只有一个范围键的第一个问题,您可以使用本地二级索引 . 您将普通属性指定为LSI的范围键,DynamoDB将通过比较该属性对您的行(使用相同的哈希键)进行排序 . 所以LSI基本上给你“额外的rangeKey” . 您最多可以创建5个LSI .

    有关查询LSI的示例,请参阅herehere . 您可以像处理常规表一样处理索引 . 您可以对索引进行查询和扫描(但不能放置) .

    关于关于全局排序行而不是使用相同的hashkey排序项的第二个问题,我不认为DynamoDB支持开箱即用的这个功能 . 你不得不

    a) scan and sort the items on your own
    b) or create a global secondary index with just one hash key and dump all your items into that hashkey. It is not recommended because this creates a hot partition in GSI.
    c) or design your schema to avoid having to sort items globally.
    

相关问题