动态表索引设计全球索引或本地索引 [英] dynamodb table index design gloabl index or local

查看:95
本文介绍了动态表索引设计全球索引或本地索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个dynamodb表,例如:

I have 3 dynamodb tables, like:

  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上载的,因此它可以上载许多用户,所以:

basically a company record would have many address and a user will belong to only one company, this user was uploaded using a unique upload_id that could have uploaded many users, so:

如果我想让所有具有特定upload_id的用户使用,那么全局索引会更好吗?

if i want to get all the users who have a specific upload_id, is a Global Index better?

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

and if i want to get all users from one company, would a Local Secondary Index better?

推荐答案

仅在要求索引高度一致的情况下才应使用本地二级索引(LSI).如果您对最终一致的索引感到满意,则应使用全局二级索引(GSI),因为LSI有很多限制.

You should only use a Local Secondary Index (LSI) if you require the index to be strongly consistent. If you are okay with an eventually consistent index, then you should use a Global Secondary Index (GSI) because LSIs have a lot of limitations.

  • 如果不删除并重新创建整个表,则无法修改或删除LSI.可以随时创建/删除GSI,而不会影响主表.
  • LSI使每个分区密钥的数据限制为10 GB.如果没有LSI,则每个分区限制为10 GB,但是您不会注意到,因为DynamoDB可以根据需要将单个分区键的数据拆分为多个分区.
  • 使用LSI时,400kb的项目大小限制适用于该项目及其所有LSI投影. GSI中的项目与主表中的项目分开计算.
  • GSI的预定容量可以独立于主表进行扩展,而LSI与基本表共享相同的容量.
  • LSIs cannot be modified or deleted without deleting and recreating the whole table. GSIs can be created/removed at any time with no impact to the main table.
  • LSIs cause you to have a limit of 10 GB of data per partition key. Without a LSI, there is a limit of 10 GB per partition, but you won’t notice that because DynamoDB can split your data for a single partition key across multiple partitions if necessary.
  • When using a LSI, the 400kb item size limit applies to the item plus all of its LSI projections. An item in a GSI is counted separately from the item in the main table.
  • Provisioned capacity of GSIs can be scaled independently of the main table, whereas LSIs share the same capacity as the base table.

有关更多信息,请参见使用二级索引改善数据访问细节.

See Improving Data Access with Secondary Indexes for more details.

这篇关于动态表索引设计全球索引或本地索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆