我有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
的用户,全球索引更好吗?
如果我想从一个公司获得所有用户,本地二级索引会更好吗?
答案 0 :(得分:0)
仅在要求索引高度一致时,才应使用本地二级索引(LSI)。如果您对最终一致的索引感到满意,则应使用全局二级索引(GSI),因为LSI有很多限制。
有关更多详细信息,请参见Improving Data Access with Secondary Indexes。