读取 DynamoDB 的不同本地二级索引键 table

Reading distinct local secondary index keys of a DynamoDB table

假设我有 staff table,主键由 organization_id 作为 分区键 staff_id 作为 排序键。另一方面,我有 department 本地二级索引,department_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 table 检索此列表?我不喜欢维护另一个 departments table。我是 DynamoDB 的新手,所以如果您对整体 table 设计有任何 comments/advices,我们非常欢迎。

截至目前,DynamoDB 中没有直接的 distinct 功能,但您可以通过以下方式实现。

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

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

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

谢谢