amazon-dynamodb - dynamodb 表索引设计 全局索引或本地
问题描述
我有 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 有很多限制。
- 如果不删除并重新创建整个表,则无法修改或删除 LSI。可以随时创建/删除 GSI,而不会影响主表。
- LSI 会导致每个分区键的数据限制为 10 GB 。如果没有 LSI,每个分区有 10 GB 的限制,但您不会注意到这一点,因为 DynamoDB 可以在必要时将单个分区键的数据拆分为多个分区。
- 使用 LSI 时,400kb 项目大小限制适用于项目及其所有 LSI 投影。GSI 中的项目与主表中的项目分开计算。
- GSI 的预置容量可以独立于主表进行扩展,而 LSI 与基表共享相同的容量。
有关更多详细信息,请参阅使用二级索引改进数据访问。
推荐阅读
- image - flutter release APK不显示图片资源
- r - 当我们在 R 中有 r 和 p 值时,如何为 pearsons 计算多重相关的回归线斜率线
- python - 使用 django 在 digitalocean 上动态添加 cron 任务
- python - 如何使用 django-background-task 模块使用 Supervisord
- docfx - 如何配置 DOCFX 以在每个页面上显示源代码
- javascript - 函数多次触发
- java - 如何使用 Commons CSV 使用 Java 删除 csv 文件中的重复项?
- django - view.py 是干什么用的?
- android - 我无法验证我的新活动
- javascript - 调用 reddit api 以获取访问令牌被 CORS 策略阻止