dynamodb 中的 scan 和 query 有什么区别?什么时候使用扫描/查询? [英] What is the difference between scan and query in dynamodb? When use scan / query?

查看:24
本文介绍了dynamodb 中的 scan 和 query 有什么区别?什么时候使用扫描/查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DynamoDb 文档中指定的查询操作:

A query operation as specified in DynamoDb documentation:

查询操作仅搜索主键属性值,并支持键属性值的比较运算符子集以优化搜索过程.

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.

根据性能和成本考虑,哪个最好.

Which is best based on Performance and Cost Considerations.

推荐答案

在创建 Dynamodb 表时选择主键和本地二级索引 (LSI),以便查询操作返回您想要的项目.

When creating a Dynamodb table select Primary Keys and Local Secondary Indexes (LSIs) so that a Query operation returns the items you want.

查询操作仅支持对主键进行相等运算符评估,但对排序键有条件(=、<、<=、>、>=、Between、Begin).

Query operations only support an equal operator evaluation of the Primary Key, but conditional (=, <, <=, >, >=, Between, Begin) on the Sort Key.

扫描操作通常更慢且成本更高,因为该操作必须遍历表中的每个项目以获取您请求的项目.

Scan operations are generally slower and more expensive as the operation has to iterate through each item in your table to get the items you are requesting.

例子:

Table: CustomerId, AccountType, Country, LastPurchase

Primary Key: CustomerId + AccountType

在本例中,您可以使用 Query 操作来获取:

In this example, you can use a Query operation to get:

  1. 在 AccountType 上带有条件过滤器的 CustomerId

需要使用扫描操作返回:

A Scan operation would need to be used to return:

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

为避免对频繁使用的操作进行扫描操作,请创建本地二级索引 (LSI) 或全局二级索引 (GSI).

To avoid scan operations on frequently used operations create a Local Secondary Index (LSI) or Global Secondary Index (GSI).

例子:

Table: CustomerId, AccountType, Country, LastPurchase

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

在这个例子中,一个查询操作可以让你得到:

In this example a Query operation can allow you to get:

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

这篇关于dynamodb 中的 scan 和 query 有什么区别?什么时候使用扫描/查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆