在设计DDB 性能方面,Get API 是 DDB 毫秒级数据检索能力的关键,因此围绕此 API 设计数据是合乎逻辑的
带有分区键 + 排序键的表
Partition Key | Sort Key
--------------+-------------
Course1 | Student1
Course1 | Student2
优势:
- 能够通过
Partition Key 和Sort Key 使用Get API 获取单个记录,例如获取 Partition Key = "Course1" 和 Sort Key = "Student1" 的单条记录
- 能够使用
Get API 仅通过Partition Key 获取记录列表,例如获取 Partition Key = "Course1" 的所有记录
缺点:
- 如果您只知道
Sort Key(即学生)而不知道Partition Key(即课程),您将无法使用Get API 仅通过Sort Key 检索记录
注意:一般来说,DDB Get API 查询的效率(这样查询不会轻易命中ReadThroughput 异常“屋顶”)与Partition Key 的唯一性和分布密切相关。您拥有和分散的分区键越多,性能就越好
只有分区键的表
Partition Key Only
--------------------
Course1#Student1
Course1#Student2
优势:
- 能够使用
Get API 获取Partition Key 的单个记录,例如获取 Partition Key = "Course1#Student1" 的单条记录
缺点:
- 将无法使用
Get API 获取仅使用 Partition key 子集的记录列表,例如获取 Partition Key = "Course1" 的记录列表
关于 GSI
注意:在表上添加 全局二级索引 以支持使用备用键的 Get API 调用是一种常见的场景,例如 Get a List of records where @987654342 @ = 课程名称
Partition Key Only | Non Key Attribute (Course) For GSI
---------------------+---------------------------
Course1#Student1 | Course1
Course1#Student2 | Course1
您最多可以拥有 20 个 GSI 索引(软限制),可以选择通过支持请求删除此限制
Partition Key Only | Non Key Attribute (Course) For GSI | Lecturer (For GSI 2)
---------------------+------------------------------------+---------------------
Course1#Student1 | Course1 | Lecturer1
Course1#Student2 | Course1 | Lecturer1
结论
如果性能是关键,我会为Partition Keys 设计一个尽可能多的唯一 值,即分区键 = Course1#Student1 VS 分区键 = Course1,排序键 = Student1
如果您需要通过备用键查询,请将GSIs 添加到表中按需
(过去GSIs 被限制为每个表 5 个,并且必须在创建表时指定,但这些限制已被取消)