Optimizing Update Query

Hi Guys

Wanted to get ideas as to how an update query can be optimized say for PostgreSQL. Instead of updating all the fields after checking if they changed. Any inputs? Thanks.

Do you mean you first make a SELECT, execute some logic to decide if a row has changed, and only then execute an UPDATE? I suggest to just UPDATE.

I am working on an api to update certain fields in a table.

On UPDATE/PUT call, only those fields that have changed will be sent. I want to update only those fields. How can this be done?

UPDATE will tak care of this. If you have a table

CREATE TABLE t (f1 integer, f2 varchar(16));

and you insert a row

INSERT INTO t (f1, f2) values (1, 'foo');

and later update this row using

UPDATE t set f1 = 1, f2 = 'bar' where f1 = 1;

only f2 will be changed although f1 is also mentioned in the set clause. But since the value of f1 did not change, PostgreSQL will not change it.

Just make an UPDATE including all fields and don’t care if some of these did not really change.

2 Likes

Thanks @lutzhorn

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.