dynamodb表索引设计全局索引或本地

问题描述 投票:0回答:1

我有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}

基本上公司记录会有很多地址而用户只属于一家公司,这个用户是使用可以上传了很多用户的唯一upload_id上传的,所以:

如果我想让所有拥有特定upload_id的用户,全球指数更好吗?

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

amazon-dynamodb
1个回答
0
投票

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

  • 如果不删除并重新创建整个表,则无法修改或删除LSI。可以随时创建/删除GSI,而不会影响主表。
  • LSI会导致每个分区键的数据限制为10 GB。如果没有LSI,每个分区的限制为10 GB,但您不会注意到,因为如果需要,DynamoDB可以跨多个分区拆分单个分区密钥的数据。
  • 使用LSI时,400kb项目大小限制适用于项目及其所有LSI预测。 GSI中的项目与主表中的项目分开计算。
  • GSI的预置容量可以独立于主表进行扩展,而LSI与基表共享相同的容量。

有关详细信息,请参阅Improving Data Access with Secondary Indexes

© www.soinside.com 2019 - 2024. All rights reserved.