【问题标题】:How to sort DynamoDB rows by an attribute如何按属性对 DynamoDB 行进行排序
【发布时间】:2018-11-29 02:44:48
【问题描述】:

我有下表:

ItemUUID (string) | Purchases (number) | Type (string)
--------------------------------------
book-a | 5 | book
book-b | 1 | book
book-c | 10 | book

每次有人购买商品时,我都想增加Purchases。然后我想查询该表以获取购买最多的商品(ItemUUIDs 按Purchases 排序 DESC)

我想我可以将ItemUUID 设为主键,并在Type 上使用Purchases 的排序键创建全局二级索引。然后查询Type = book所在的GSI,将排序键留空,降序排序。

这不起作用。 DynamoDB 是否可以使用我的用例?

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb dynamodb-queries amazon-dynamodb-index


    【解决方案1】:

    我想出的作品,但我觉得有点hack'ish。

    表定义

    Primary partition key   type (String)
    Primary sort key        uuid (String)
    

    全球二级指数

    Primary partition key   type (String)
    Primary sort key        purchases (Number)
    

    因为 GSI 不必具有唯一的分区+排序键,我可以通过查询 type=book,将购买留空,对 DESC 排序

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 2020-10-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      相关资源
      最近更新 更多