Amazon dynamodb 如何在不指定范围键值的情况下对DynamoDB查询结果进行排序

Amazon dynamodb 如何在不指定范围键值的情况下对DynamoDB查询结果进行排序,amazon-dynamodb,dynamodb-queries,Amazon Dynamodb,Dynamodb Queries,我正在构建一个DynamoDB表,并且遇到了如何最好地构造索引的问题。我需要执行3个查询 我的桌子: AttributeDefinitions: # This is large groups that can have many events - AttributeName: groupId AttributeType: S # An event can have many actions - AttributeName: eventId AttributeTyp

我正在构建一个DynamoDB表,并且遇到了如何最好地构造索引的问题。我需要执行3个查询

我的桌子:

AttributeDefinitions:
  # This is large groups that can have many events
  - AttributeName: groupId
    AttributeType: S
  # An event can have many actions
  - AttributeName: eventId
    AttributeType: S
  # Each item has a unique actionId
  - AttributeName: actionId
    AttributeType: S
  # Each item has a creation date
  - AttributeName: createdAt
    AttributeType: S
  # Some type I need to filter by (enum: trigger|task for example)
  - AttributeName: actionType
    AttributeType: S

# Main query to return items by action ID - that works fine
KeySchema:
  - AttributeName: groupId
    KeyType: HASH
  - AttributeName: actionId
    KeyType: RANGE
以下是我需要实现的3个查询:

  • 取一件物品
  • 现在,我使用

    Key: {
      groupId,
      actionId
    }
    
    效果很好

  • 按eventId获取所有项(操作)
  • SQL:

    如果我做这个本地索引,那么效果很好:

    KeySchema:
      - AttributeName: groupId
        KeyType: HASH
      - AttributeName: eventId
        KeyType: RANGE
    
  • 获取actionType='trigger'由createdAt date排序的、属于groupId的所有项目(不考虑eventId)
  • SQL:

    这是一个给我问题。我想查询我的数据并按日期排序返回。但是,我需要使用另一个字段作为范围进行查询。因此,如果我添加createdAt作为我的范围,我不能使用actionType进行筛选。如果我使用actionType,那么就没有排序


    我怎样才能最好地安排这张桌子?就数据量而言。可以有许多组(groupId)。每个组可以有许多事件(eventId)。但是每个事件可能只有才能实现类似的查询
    SELECT*在DynamoDB中,从actionType='trigger'和groupId=123按createdAt排序的表中,您需要有一个索引,其中散列键为
    groupId
    ,复合排序键为
    actionTypeCreatedAt
    (可以预见,它是actionType、分隔符,然后是createdAt日期)

    在索引中,数据如下所示(假设排序键中的分隔符为“u”):


    现在,要实现您想要的查询,您需要使用
    groupId=123的键条件表达式,并以(actionTypeCreatedAt,“trigger\uu”)
    )开头。DynamoDB将根据排序键自动对结果进行排序,由于所有查询结果都具有相同的
    actionType
    前缀,结果将仅按
    createdAt
    日期排序。

    您现在使用的确切关键条件表达式是什么?@MatthewPope您的问题帮助我更清楚地看到了我的问题-事实上,我可以在不使用createdAt值的情况下进行查询,但不能使用所需的过滤器。我已经编辑了这个问题。那么,您是在询问如何在DynamoDB查询中指定排序,还是在询问如何设计表以启用所需的查询?如果是后者,将查询列为英语句子或SQL可能会有所帮助。我添加了一个SQL来说明。我想我是否需要查询或索引结构方面的帮助取决于需要做什么。希望sql能澄清这一点?这是您需要执行的唯一查询吗?如果还有其他的,你能把它们也列出来吗?使用DynamoDB,您可以获得很好的性能,但需要权衡的是,您需要在设计表时考虑所有的访问模式。啊。。因此,当我放置一个项目时,将手动创建actionType、createdAt和actionTypeCreatedAt字段是否正确?是的,在将项目发送到DynamoDB之前,必须在应用程序中填充该字段。
    KeySchema:
      - AttributeName: groupId
        KeyType: HASH
      - AttributeName: eventId
        KeyType: RANGE
    
    SELECT * FROM theTable WHERE actionType = 'trigger' AND groupId = 123 SORT BY createdAt
    
    groupId | actionTypeCreatedAt
    --------|------------------------------
        123 | trigger_2019-06-30T08:30:00Z
        123 | trigger_2019-07-05T23:00:00Z
        123 | trigger_2019-07-20T10:15:00Z
        123 | action2_2019-06-25T15:10:00Z
        123 | action2_2019-07-08T02:45:00Z