amazon-web-services - dynamodb 中的扫描和查询有什么区别?什么时候使用扫描/查询?

标签 amazon-web-services amazon-dynamodb

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

在此示例中,您可以使用查询操作来获取:

  1. 对 AccountType 进行条件过滤的 CustomerId

需要使用扫描操作来返回:

  1. 具有特定 AccountType 的所有客户
  2. 基于国家/地区条件过滤器的项目,即来自美国的所有客户
  3. 基于 LastPurchase 的条件过滤器的商品,即上个月进行过购买的所有客户

为了避免对常用操作进行扫描操作,请创建本地二级索引 (LSI) 或全局二级索引 (GSI)。

示例:

Table: CustomerId, AccountType, Country, LastPurchase

Primary Key: CustomerId + AccountType
GSI: AccountType + CustomerId
LSI: CustomerId + LastPurchase

在此示例中,查询操作可以让您获取:

  1. 对 AccountType 进行条件过滤的 CustomerId
  2. [GSI] 针对特定 AccountType 的 CustomerId 的条件过滤器
  3. [LSI] 在 LastPurchase 上带有条件过滤器的 CustomerId

关于amazon-web-services - dynamodb 中的扫描和查询有什么区别?什么时候使用扫描/查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43452219/

相关文章:

php - 使用 AWS 开发工具包 PHP 的 DynamoDB 中的保留字

javascript - Amazon S3 客户端加密 Javascript

amazon-dynamodb - 是否可以更新 DynamoDB 中主键的属性值?

python - boto3 Dynamodb 表批量插入缺少一些东西

amazon-dynamodb - 无法解析 JSON 文档 : Unrecognized token '$util' : was expecting ('true' , 'false' 或 'null' )

node.js - Aws Cognito 联合身份在 getOpenIdTokenForDeveloperIdentity() 上回答 UnknownError Not Found

javascript - AWS Cloudwatch 获取警报名称

amazon-web-services - 创建新存储桶或使用 OpenBD 检查存储桶是否存在时,某些功能似乎不存在

amazon-web-services - AWS DynamoDB 向后扫描忽略 LastEvaluatedKey

amazon-web-services - DynamoDB 多个筛选条件,给出错误 - buildTree 错误 : unset parameter: ConditionBuilder