Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XWnJn-00046E-BU for pgsql-performance@arkaria.postgresql.org; Wed, 24 Sep 2014 14:13:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XWnJm-0005v8-S0 for pgsql-performance@arkaria.postgresql.org; Wed, 24 Sep 2014 14:13:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XWnJm-0005v0-0c for pgsql-performance@postgresql.org; Wed, 24 Sep 2014 14:13:18 +0000 Received: from oldperseverance.encs.concordia.ca ([132.205.96.92]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XWnJf-0004Ct-Ec for pgsql-performance@postgresql.org; Wed, 24 Sep 2014 14:13:16 +0000 Received: from cacao.encs.concordia.ca (emilu@cacao.encs.concordia.ca [132.205.47.225]) by oldperseverance.encs.concordia.ca (envelope-from emilu@encs.concordia.ca) (8.13.7/8.13.7) with ESMTP id s8OED5Tj002261; Wed, 24 Sep 2014 10:13:07 -0400 Message-ID: <5422D171.5060801@encs.concordia.ca> Date: Wed, 24 Sep 2014 10:13:05 -0400 From: Emi Lu Reply-To: emilu@encs.concordia.ca User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.8.0 MIME-Version: 1.0 To: Heikki Linnakangas , "pgsql-performance@postgresql.org" Subject: Re: Which update action quicker? References: <5421D9FB.2030008@encs.concordia.ca> <5422CBB4.3000301@vmware.com> In-Reply-To: <5422CBB4.3000301@vmware.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.58 on oldperseverance.encs.concordia.ca at 2014/09/24 10:13:08 EDT X-Pg-Spam-Score: -2.6 (--) 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 Hello, > For a big table with more than 10 Million 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, c2...c_N) <> (a.c1, c2... 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. The situation is: (t1.c1, c2, ... c_N) <> (a.c1, c2...c_N) won't return too many diff records. So, the calculation will only be query most of the case. But if truncate/delete and copy will cause definitely write all more than 10 million data. If for situation like this, will it still be quicker to delete/insert quicker? Thank you Emi -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance