我有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
的用户,全球指数更好吗?
如果我想从一家公司获得所有用户,本地二级指数会更好吗?
如果要求索引具有很强的一致性,则只应使用本地二级索引(LSI)。如果您对最终一致的索引没有问题,那么您应该使用全局二级索引(GSI),因为LSI有很多限制。
有关详细信息,请参阅Improving Data Access with Secondary Indexes。