0

假设我有一个staff主键组成的表作为organization_id分区排序键。另一方面,我有作为排序键的本地二级索引。staff_iddepartmentdepartment_id

{
  ...
  KeySchema: [
    { AttributeName: "organization_id", KeyType: "HASH"},
    { AttributeName: "staff_id", KeyType: "RANGE" }
  ],
  LocalSecondaryIndexes: [
    {
        IndexName: "department",
        KeySchema: [
            { AttributeName: "organization_id", KeyType: "HASH"},
            { AttributeName: "department_id", KeyType: "RANGE" }
        ],
        Projection: {
            ProjectionType: "KEYS_ONLY"
        }
    }
  ],
  AttributeDefinitions: [
    { AttributeName: "organization_id", AttributeType: "S" },
    { AttributeName: "staff_id", AttributeType: "S" },
    { AttributeName: "department_id", AttributeType: "S" }
  ]
  ...
}

很容易发现,有许多不同的项目staff_id共享相同的department索引键。我需要用给定的组织查询部门列表organization_id。有没有办法从staff表中检索这个列表?我不喜欢维护另一个departments表。我是 DynamoDB 的新手,所以如果您对整体表设计有任何意见/建议,非常欢迎。

4

1 回答 1

2

到目前为止,DynamoDB 中还没有 distinct 的直接特征,但您可以通过以下方式实现。

  1. 查询 LSI 并获取 organizationId 的所有记录,然后在应用程序级别找到不同的值。(这在 NoSql DB 中很常见)

  2. 正如您提到的创建另一个表,现在我建议您创建另一个表,这样您就可以直接检索仅选定的值

  3. CloudSearch:可以集成到 DynamoDB 表中,然后您可以直接在 Cloudsearch 中搜索而不是在表中搜索,但是当您有多个搜索查询和数百万条记录时,这很有用。

谢谢

于 2017-09-12T04:41:23.053 回答