[英]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.