简体   繁体   English

有什么更有效的? 读取和写入是否……或始终写入数据库?

[英]What's more efficient? Read and Write If… or always write to db?

I have a database table, that has a column, which is being updated frequently (relatively). 我有一个数据库表,其中有一个列,该列经常(相对)进行更新。

The question is: 问题是:

Is it more efficient to avoid always writing to the database, by reading the object first ( SELECT ... WHERE ), and comparing the values, to determine if an update is even necessary 通过首先读取对象( SELECT ... WHERE )并比较值以确定是否甚至需要更新来避免始终写入数据库是否更有效率SELECT ... WHERE

or always just issue an update ( UPDATE ... WHERE ) without checking what's the current state. 或者总是只发出更新( UPDATE ... WHERE )而无需检查当前状态是什么。

I think that the first approach would be more hassle, as it consists of two DB operations, instead of just one, but we could also avoid an unnecessary write. 我认为第一种方法比较麻烦,因为它包含两个数据库操作,而不只是一个,但我们也可以避免不必要的写入。

I also question if we should even think about this, as our db will most likely not reach the 100k records in this table anytime soon, so even if the update would be more costly, it wouldn't be an issue, but please correct me if I'm wrong. 我还问我们是否应该考虑一下,因为我们的数据库很可能很快就不会达到该表中的100k记录,所以即使更新成本更高,也不会有问题,但是请纠正我如果我错了。

The database is PostgreSQL 9.6 数据库是PostgreSQL 9.6

It will avoid I/O load on the database if you only perform the updates that are necessary. 如果仅执行必要的更新,它将避免数据库上的I / O负载。

You can include the test with the UPDATE , like in 您可以将测试包含在UPDATE ,例如

UPDATE mytable
SET mycol = 'avalue'
WHERE id = 42
  AND mycol <> 'avalue';

The only downside is that triggers will not be called unless the value really changes. 唯一的缺点是,除非该值确实发生更改,否则不会调用触发器。

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

相关问题 读写存储在AWS S3(Heroku + Django)中的数据库文件 - Read and write a DB file stored in aws s3 (Heroku + Django) 什么是编写此函数声明的更Python方式 - What's a more pythonic way to write this function declaration 如何在django 0.97上分开读取数据库服务器和写入数据库服务器? - How to separate read db server and write db server on django 0.97? Django / SQL:最有效的方法是,有更多的列或更多的表? - Django/SQL: what's most efficient, more columns or more tables? serializers.StringRelatedField的读/写等价物是什么? - What is the read/write equivalent of serializers.StringRelatedField? 有没有一种更有效的方法,可以根据条件数量的参数在Django中编写此自定义SQL? - Is there a more efficient way to write this custom SQL in Django based on a conditional number of arguments? 什么是龙卷风在django中“写”的替代解决方案? - what's the alternative solution of tornado's “write” in django? Django-compressor:如何写入S3,从CloudFront读取? - Django-compressor: how to write to S3, read from CloudFront? Memcache数据库模型可提高搜索效率 - Memcache db models to make search more efficient 在没有虚拟form.Form的情况下创建无字段表单的更有效方法是什么? - What's a more efficient way to create a field-less form without a dummy forms.Form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM