Amazon dynamodb dynamodb表索引设计gloabl索引或本地

Amazon dynamodb dynamodb表索引设计gloabl索引或本地,amazon-dynamodb,Amazon Dynamodb,我有3张dynamodb表格,如: companies: Type: AWS::DynamoDB::Table Properties: TableName: ${self:provider.region}.${opt:stage}.companies AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema:

我有3张dynamodb表格,如:

  companies:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: ${self:provider.region}.${opt:stage}.companies
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      Tags:
        - Key: Name
          Value: ${self:provider.region}.${opt:stage}.${self:custom.customDomain.domainName}
  addresses:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: ${self:provider.region}.${opt:stage}.addresses
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      Tags:
        - Key: Name
          Value: ${self:provider.region}.${opt:stage}.${self:custom.customDomain.domainName}
  users:
    Type: AWS::DynamoDB::Table
    DependsOn: companies
    Properties:
      TableName: ${self:provider.region}.${opt:stage}.users
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
        - AttributeName: email
          AttributeType: S
        - AttributeName: upload_id
          AttributeType: S
        - AttributeName: company
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
        - AttributeName: email
          KeyType: RANGE
      LocalSecondaryIndexes:
        - IndexName: LSI-${self:provider.region}-${opt:stage}-companyId-by-userId-index
          KeySchema:
            - AttributeName: company
              KeyType: HASH
            - AttributeName: id
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
      GlobalSecondaryIndexes:
        - IndexName: GSI-${self:provider.region}-${opt:stage}-uploadId-by-userId-index
          KeySchema:
            - AttributeName: upload_id
              KeyType: HASH
            - AttributeName: id
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
      Tags:
        - Key: Name
          Value: ${self:provider.region}.${opt:stage}.${self:custom.customDomain.domainName}
基本上,一个公司记录会有多个地址,一个用户只属于一个公司,该用户是使用唯一的上载id上载的,该id可以上载多个用户,因此:

如果我想获得所有具有特定上传id的用户,全局索引更好吗


如果我想从一家公司获取所有用户,本地二级索引会更好吗?

如果您要求索引具有强一致性,则只能使用本地二级索引LSI。如果您对最终一致的索引没有意见,那么应该使用全局二级索引GSI,因为LSI有很多限制

如果不删除并重新创建整个表,则无法修改或删除LSI。可以随时创建/删除GSI,而不会影响主表。 LSI导致每个分区密钥的数据限制为10 GB。如果没有LSI,每个分区有10GB的限制,但您不会注意到这一点,因为DynamoDB可以在必要时将单个分区密钥的数据分割到多个分区。 使用LSI时,400kb项目大小限制适用于该项目及其所有LSI投影。GSI中的项目与主表中的项目分开计算。 GSI的调配容量可以独立于主表进行扩展,而LSI与基表共享相同的容量。 有关更多详细信息,请参阅