简体   繁体   English

在将项目添加到 DynamoDB 表时我应该使用 put 还是 update 命令

[英]Should I use put or update command while adding an item to DynamoDB table

With the year as a Primary Key and the item being:year作为主键,项目为:

let item = {

    "year": 2021,
    "title": "Title A"
}

I can go ahead and add it to the DynamoDB using put method:我可以提前 go 并使用put方法将其添加到 DynamoDB:

var AWS = require('aws-sdk');
AWS.config.update({region: "us-east-1"});
const docClient = new DynamoDB.DocumentClient();

var params = {
  TableName: TABLE_NAME,
  Item: item,
  ConditionExpression: "year <> :year",
  ExpressionAttributeValues: {":year": 2021}
};
let promise = docClient.put(params).promise();
promise.then(res => {
  console.log("res:", res);
});

Please note, that I am using ConditionExpression here to assure that the item with the same Primary Key year 2021 does not already exist in the table.请注意,我在这里使用ConditionExpression来确保表中不存在具有相同主键year 2021 的项目。 If the item exists, I will be getting the error.如果该项目存在,我将收到错误消息。 Otherwise, the item will be added and the empty dictionary is returned.否则,将添加该项目并返回空字典。

Instead of using the put method I could get the same functionality using the update command, such as:我可以使用update命令获得相同的功能,而不是使用put方法,例如:

let params = {
  TableName: TABLE_NAME,
  Key: {"year": year},
  UpdateExpression: "SET year=:year",
  ExpressionAttributeValues: {":year": year},
  ConditionExpression: "year <> :year"
};  
let promise = docClient.update(params).promise();
promise.then(res => {
  console.log("res:", res);
});

While both put and update commands here do the job well, I tend to like the update command more as it allows me to specify "ALL_NEW" as an argument to ReturnValues parameter (for the put command the option "ALL_NEW" is not available).虽然这里的putupdate命令都能很好地完成工作,但我更喜欢update命令,因为它允许我将“ALL_NEW”指定为ReturnValues参数的参数(对于put命令,选项“ALL_NEW”不可用)。

Is there any performance drop in using the update command versus the put ?使用update命令与put相比是否有任何性能下降? Is there any reason why I should be using put instead of update ?有什么理由我应该使用put而不是update吗?

There is no performance diff, but there is a difference regarding throughput.没有性能差异,但在吞吐量方面存在差异。

  • PutItem - Writes a single item to a table. PutItem - 将单个项目写入表。 If an item with the same primary key exists in the table, the operation replaces the item.如果表中存在具有相同主键的项,则操作替换该项。 For calculating provisioned throughput consumption, the item size that matters is the larger of the two.对于计算预置吞吐量消耗,重要的项目大小是两者中的较大者。

  • UpdateItem - Modifies a single item in the table. UpdateItem - 修改表中的单个项目。 DynamoDB considers the size of the item as it appears before and after the update. DynamoDB 会考虑更新前后显示的项目大小。 The provisioned throughput consumed reflects the larger of these item sizes.消耗的预置吞吐量反映了这些项目大小中的较大者。 Even if you update just a subset of the item's attributes, UpdateItem will still consume the full amount of provisioned throughput (the larger of the "before" and "after" item sizes).即使您仅更新项目属性的子集,UpdateItem 仍将消耗全部预置吞吐量(“之前”和“之后”项目大小中的较大者)。

Above quote is from the official AWS docs(see references)以上引用来自官方 AWS 文档(请参阅参考资料)

However everything depends what you are doing:然而,一切都取决于你在做什么:

PUT item replace whole entry in DynamoDB. PUT 项目替换 DynamoDB 中的整个条目。 UPDATE can update only single attribute. UPDATE 只能更新单个属性。 You can update attribute with PUT as well.您也可以使用 PUT 更新属性。

UPDATE of single attribute == GET -> Modify single attribute in the code -> PUT record data to DB更新单个属性 == GET -> 修改代码中的单个属性 -> PUT 记录数据到 DB

References:参考:

The main difference for me is that you can add a ConditionExpression so that you won't accidentally overwrite a key that already exists.对我而言,主要区别在于您可以添加一个 ConditionExpression,这样您就不会意外覆盖已经存在的键。 Example:例子:

table.update_item(
        Key={
            'ID': '1'
        },
        UpdateExpression='SET #a= :a, #b= :b',
        ConditionExpression='attribute_exists(ID)',
        ExpressionAttributeValues={
            ':a': json.dumps(payload),
            ':b': str(datetime.now())
        },
        ExpressionAttributeNames={
            '#a': 'attribute1',
            '#b': 'attribute2'
        },
        ReturnValues='ALL_NEW'
    )

This example will only perform the update if the item actually exists.此示例仅在项目实际存在时才执行更新。 You can change it to attribute_not_exists(ID) to insert only if that key does not exist.您可以将其更改为 attribute_not_exists(ID) 以仅在该键不存在时插入。 I feel this gives you better control of when/when not to push data into your table.我觉得这可以让您更好地控制何时/何时不将数据推送到您的表中。

In short, in this particular case, there is no difference.简而言之,在这种特殊情况下,没有区别。

put and update have the same behaviour when the item doesn't exist in the table.当项目在表中不存在时, putupdate具有相同的行为。 Regrading to the throughput difference mentioned by @Daniel Hornik, I disagree.对于@Daniel Hornik 提到的吞吐量差异,我不同意。 In this case, they both consume the same WCU.在这种情况下,它们都使用相同的 WCU。 Therefore, if you use provisioned throughput for writes, you can expect the same level of throughput.因此,如果您使用预置吞吐量进行写入,则可以预期相同级别的吞吐量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM