简体   繁体   English

保存django ORM模型没有变化有多贵?

[英]how expensive is to save a django ORM Model without changes?

sometimes we have to do a Model instance.save() regardless if some field changed, just for security and fast development. 有时我们必须做一个Model instance.save(),无论是否有一些字段发生变化,只是为了安全和快速开发。

  1. how expensive is this with django ORM? django ORM有多贵?
  2. signals are always sent? 信号总是发送?
  3. any SQL query is executed? 是否执行了任何SQL查询?

I tested with django debug toolbar to do 10 .save() in different points where anything in the model has changed, and the log does not register sql queries. 我使用django调试工具栏测试了在模型中的任何内容发生更改的不同点上执行10 .save(),并且日志不会注册sql查询。

other way to test it or some article? 测试它或一些文章的其他方法?

thank you in advance. 先感谢您。

Im not entirely sure how you application handles this. 我不完全确定你的应用程序如何处理这个。

But i ran a small test: 但我跑了一个小测试:

a = Blog.objects.get(pk=1)

for b in range(1, 100):
    a.save()

This gave me a result of: 这给了我一个结果:

87.04 ms (201 queries) 87.04毫秒(201个查询)

Be ware as well that a save will do two queries: 要保存,保存将执行两个查询:

SELECT ••• FROM `fun_blog` WHERE `fun_blog`.`id` = 1 LIMIT 1

UPDATE `fun_blog` SET `title` = 'This is my testtitle', `body` = 'This is a testbody' WHERE `fun_blog`.`id` = 1

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

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