Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XWmwB-0003EP-1B for pgsql-performance@arkaria.postgresql.org; Wed, 24 Sep 2014 13:48:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XWmwA-0003kF-Hw for pgsql-performance@arkaria.postgresql.org; Wed, 24 Sep 2014 13:48:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XWmw9-0003k9-Pb for pgsql-performance@postgresql.org; Wed, 24 Sep 2014 13:48:53 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XWmw5-0006Rb-2z for pgsql-performance@postgresql.org; Wed, 24 Sep 2014 13:48:52 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id BD2E398884; Wed, 24 Sep 2014 06:48:43 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (EX13-CAS-003.vmware.com [10.113.191.53]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id B40B7B1521; Wed, 24 Sep 2014 06:48:43 -0700 (PDT) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-004.vmware.com (10.113.191.24) with Microsoft SMTP Server (TLS) id 15.0.775.38; Wed, 24 Sep 2014 06:48:41 -0700 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.775.38; Wed, 24 Sep 2014 06:48:40 -0700 Message-ID: <5422CBB4.3000301@vmware.com> Date: Wed, 24 Sep 2014 16:48:36 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.0 MIME-Version: 1.0 To: , "pgsql-performance@postgresql.org" Subject: Re: Which update action quicker? References: <5421D9FB.2030008@encs.concordia.ca> In-Reply-To: <5421D9FB.2030008@encs.concordia.ca> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -7.8 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org 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