首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态数据库表索引设计、全局索引或局部索引

动态数据库表索引设计、全局索引或局部索引
EN

Stack Overflow用户
提问于 2019-04-02 23:00:59
回答 1查看 487关注 0票数 0

我有3个dynamodb表,比如:

代码语言:javascript
复制
  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可以上传很多用户,所以:

如果我想得到所有的用户谁有一个特定的upload_id,是一个全球指数更好吗?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-03 00:27:03

只有在要求索引强烈一致时,才应使用本地辅助索引(LSI)。如果您对最终一致的索引没有意见,那么您应该使用全局二级索引(GSI),因为LSI有很多限制。

  • 如果不删除和重新创建整个表,则无法修改或删除LSIs。GSIs可以在任何时候创建/删除,不会对主表产生影响。
  • LSIs使每个分区键的数据限制为10 GB。如果没有LSI,每个分区的容量限制为10 GB,但您不会注意到这一点,因为如果需要,DynamoDB可以将单个分区键的数据拆分为多个分区。
  • 当使用LSI时,400 to的项目大小限制适用于项目加上其所有LSI投影。GSI中的项与主表中的项分开计算。
  • GSI的供应容量可以独立于主表进行缩放,而LSI与基表共享相同的容量。

有关更多详细信息,请参阅利用二次索引改进数据访问

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55484683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档