DynamoDB 文档中指定的查询操作:
A query operation searches only primary key attribute values and supports a subset of comparison operators on key attribute values to refine the search process.
和扫描操作:
A scan operation scans the entire table. You can specify filters to apply to the results to refine the values returned to you, after the complete scan.
根据性能和成本,哪一个最好?
最佳答案
创建 Dynamodb 表时,选择主键和本地二级索引 (LSI),以便查询操作返回您想要的项目。
查询操作仅支持主键的等于运算符评估,但支持排序键的条件(=、<、<=、>、>=、Between、Begin)。
扫描操作通常速度较慢且成本较高,因为该操作必须迭代表中的每个项目才能获取您请求的项目。
示例:
Table: CustomerId, AccountType, Country, LastPurchase
Primary Key: CustomerId + AccountType
在此示例中,您可以使用查询操作来获取:
- 对 AccountType 进行条件过滤的 CustomerId
需要使用扫描操作来返回:
- 具有特定 AccountType 的所有客户
- 基于国家/地区条件过滤器的项目,即来自美国的所有客户
- 基于 LastPurchase 的条件过滤器的商品,即上个月进行过购买的所有客户
为了避免对常用操作进行扫描操作,请创建本地二级索引 (LSI) 或全局二级索引 (GSI)。
示例:
Table: CustomerId, AccountType, Country, LastPurchase
Primary Key: CustomerId + AccountType
GSI: AccountType + CustomerId
LSI: CustomerId + LastPurchase
在此示例中,查询操作可以让您获取:
- 对 AccountType 进行条件过滤的 CustomerId
- [GSI] 针对特定 AccountType 的 CustomerId 的条件过滤器
- [LSI] 在 LastPurchase 上带有条件过滤器的 CustomerId
关于amazon-web-services - dynamodb 中的扫描和查询有什么区别?什么时候使用扫描/查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43452219/