django中的model.update方法是否在保存实例之前将表锁定?
我有一个场景,我需要将一个列的值复制到另一列中。我正在尝试
Model.objects.select_related('vcsdata').all().update(charging_status_v2=F('charging_status'))
使用F表达式以及更新来复制值会产生任何停机时间吗?它在执行操作时会锁定桌子吗?
I have a scenario in which I need to copy the values of one column into an another column. I am trying to do
Model.objects.select_related('vcsdata').all().update(charging_status_v2=F('charging_status'))
Does using F expression along with the update to copy the values would create any downtime? does it locks the table while performing the operation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短答案:
Django在更新过程中唯一要做的事情(是否使用
f
表达式)是否保留记录的先前状态,以防万一出现问题,它可能会回滚至先前的状态。基本上在行
中使用Transaction.mark_for_rollback_on_error(使用= self.db)
,它保留了记录的先前状态,但它不会锁定您的表格或任何类型的部分锁。例如,如果您同时有两个同时更新(假设其中一个要比另一个更长的更长,并且一个速度较慢,一个速度更快地击中您的数据库),那么一个更快的速度将击中您的数据库较慢的一个并进行操作。然后,较慢的人将在您的表上执行其他操作( 此示例足以证明更新不会锁定您的表格 )。
另请注意,呼叫更新多个对象的更新(如果是可行的话)是我据我所知(与在每个实例或批量更新上保存的调用)的最有效方法。
Short Answer:
No, it doesn't.
The only thing Django does in update process (whether you use
F
expression or not) is keeping the previous state of your record(s) in case if something goes wrong it can rollback to the previous state.Basically in the line
with transaction.mark_for_rollback_on_error(using=self.db)
, it keeps the previous state of your record, but it does not lock your table or any kind of partial locks.For example if you have two simultaneous updates at the same time, (suppose one of them is going to take much longer than the other and also slower one hits your database before faster one) then the faster one is going to hit your database regardless of the slower one and does the operation. Then slower one is going to do some other operation on your table (this example is enough for proving that update does not lock your table).
Also note that calling update for updating multiple objects (if this is a doable thing) is the most efficient way for updating multiple objects as far as I know (comparing to calling save on each instance or bulk update).