# Optimizing Update Query

**URL:** <https://forum.golangbridge.org/t/optimizing-update-query/8269>\
**Category:** Getting Help\
**Created:** [February 24, 2018, 11:21am UTC](https://forum.golangbridge.org/t/optimizing-update-query/8269 "2018-02-24T11:21:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandyethadka](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/sandyethadka/32/2442_2.png) [@sandyethadka](https://forum.golangbridge.org/u/sandyethadka)\
**Post date:** [February 24, 2018, 11:21am UTC](https://forum.golangbridge.org/t/optimizing-update-query/8269/1 "2018-02-24T11:21:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lutzhorn](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lutzhorn/32/8119_2.png) [@lutzhorn](https://forum.golangbridge.org/u/lutzhorn)\
**Post date:** [February 24, 2018, 12:20pm UTC](https://forum.golangbridge.org/t/optimizing-update-query/8269/2 "2018-02-24T12:20:02Z")

</div>

> [@sandyethadka](#):
>
> updating all the fields after checking if they changed.

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`.

---

<div class="post-metadata">

**Author:** ![sandyethadka](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/sandyethadka/32/2442_2.png) [@sandyethadka](https://forum.golangbridge.org/u/sandyethadka)\
**Post date:** [February 24, 2018, 12:26pm UTC](https://forum.golangbridge.org/t/optimizing-update-query/8269/3 "2018-02-24T12:26:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lutzhorn](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lutzhorn/32/8119_2.png) [@lutzhorn](https://forum.golangbridge.org/u/lutzhorn)\
**Post date:** [February 24, 2018, 3:20pm UTC](https://forum.golangbridge.org/t/optimizing-update-query/8269/4 "2018-02-24T15:20:01Z")

</div>

[`UPDATE`](https://www.postgresql.org/docs/10/static/sql-update.html) will tak care of this. If you have a table

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

```

and you insert a row

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

```

and later update this row using

```auto
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.

---

<div class="post-metadata">

**Author:** ![sandyethadka](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/sandyethadka/32/2442_2.png) [@sandyethadka](https://forum.golangbridge.org/u/sandyethadka)\
**Post date:** [February 24, 2018, 3:21pm UTC](https://forum.golangbridge.org/t/optimizing-update-query/8269/5 "2018-02-24T15:21:41Z")

</div>

Thanks @lutzhorn

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [May 25, 2018, 3:21pm UTC](https://forum.golangbridge.org/t/optimizing-update-query/8269/6 "2018-05-25T15:21:46Z")

</div>

Closed on May 25, 2018, 3:21pm UTC

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