Re: which Update quicker

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: which Update quicker
Date: 2014-09-23 20:39:13
Message-ID: CA+mi_8a3HqWZnGyjoxDV=RfvvapPdu44nCH06bGW+C9n967XQQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Sep 23, 2014 at 8:35 PM, Emi Lu <emilu(at)encs(dot)concordia(dot)ca> wrote:
> Hello list,
>
> For a big table with more than 1,000,000 records, may I know which update is
> quicker please?
>
> (1) update t1
> set c1 = a.c1
> from a
> where pk and
> t1.c1 <> a.c1;
> ......
> update t1
> set c_N = a.c_N
> from a
> where pk and
> t1.c_N <> a.c_N;
>
>
> (2) update t1
> set c1 = a.c1 ,
> c2 = a.c2,
> ...
> c_N = a.c_N
> from a
> where pk AND
> ( t1.c1 <> a.c1 OR t1.c2 <> a.c2..... t1.c_N <> a.c_N)

Definitely the second, and it produces less bloat too.

> Or other quicker way for update action?

You may express the comparison as (t1.c1, t1.c2, ... t1.cN) <> (t2.c1,
t2.c2, ... t2.cN)
It's not going to be faster but maybe it's more readable.

-- Daniele

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Fred Jonsson 2014-09-23 20:46:56 Question about row_number() ordering semantics
Previous Message Emi Lu 2014-09-23 19:35:09 which Update quicker