From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: emilu@encs.concordia.ca
To: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: Which update action quicker?
Date: Wed, 24 Sep 2014 16:48:36 +0300
Message-ID: <5422CBB4.3000301@vmware.com> (raw)
In-Reply-To: <5421D9FB.2030008@encs.concordia.ca>
References: <5421D9FB.2030008@encs.concordia.ca>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
On 09/23/2014 11:37 PM, Emi Lu 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)
Probably (2). <> is not indexable, so each update will have to perform a
sequential scan of the table. With (2), you only need to scan it once,
with (1) you have to scan it N times. Also, method (1) will update the
same row multiple times, if it needs to have more than one column updated.
> Or other quicker way for update action?
If a large percentage of the table needs to be updated, it can be faster
to create a new table, insert all the rows with the right values, drop
the old table and rename the new one in its place. All in one transaction.
- Heikki
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: hlinnakangas@vmware.com, emilu@encs.concordia.ca
Subject: Re: Which update action quicker?
In-Reply-To: <5422CBB4.3000301@vmware.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox