Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id C076CB5DC45 for ; Tue, 28 Jun 2011 06:49:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 82614-01-3 for ; Tue, 28 Jun 2011 09:49:05 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from server515.appriver.com (server515g.exghost.com [72.32.253.81]) by mail.postgresql.org (Postfix) with ESMTP id BD9E8B5DBFA for ; Tue, 28 Jun 2011 06:48:51 -0300 (ADT) X-Note-AR-ScanTimeLocal: 6/28/2011 4:47:09 AM X-Policy: GLOBAL - riskcontrollimited.com X-Policy: GLOBAL - riskcontrollimited.com X-Policy: GLOBAL - riskcontrollimited.com X-Policy: GLOBAL - riskcontrollimited.com X-Policy: GLOBAL - riskcontrollimited.com X-Primary: harry.mantheakis@riskcontrollimited.com X-Note: This Email was scanned by AppRiver SecureTide X-ALLOW: @riskcontrollimited.com ALLOWED X-Virus-Scan: V- X-Note: Spam Tests Failed: X-Country-Path: PRIVATE->PRIVATE->UNITED STATES->UNITED STATES X-Note-Sending-IP: 72.32.49.5 X-Note-Reverse-DNS: X-Note-WHTLIST: harry.mantheakis@riskcontrollimited.com X-Note: User Rule Hits: X-Note: Global Rule Hits: G200 G201 G202 G203 G207 G208 G219 G309 X-Note: Encrypt Rule Hits: X-Note: Mail Class: ALLOWEDSENDER X-Note: Headers Injected Received: from [72.32.49.5] (HELO FE1.exchange.rackspace.com) by server515.appriver.com (CommuniGate Pro SMTP 5.4.1c) with ESMTP id 280299209; Tue, 28 Jun 2011 04:47:09 -0500 Received: from FE4.exchange.rackspace.com ([192.168.1.28]) by FE1.exchange.rackspace.com with Microsoft SMTPSVC(6.0.3790.4675); Tue, 28 Jun 2011 04:48:50 -0500 Received: from [192.168.20.134] ([87.194.70.108]) by FE4.exchange.rackspace.com with Microsoft SMTPSVC(6.0.3790.4675); Tue, 28 Jun 2011 04:48:50 -0500 Message-ID: <4E09A389.80604@riskcontrollimited.com> Date: Tue, 28 Jun 2011 10:48:57 +0100 From: Harry Mantheakis User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.18) Gecko/20110616 Lightning/1.0b2 Thunderbird/3.1.11 MIME-Version: 1.0 To: Kevin Grittner CC: pgsql-performance@postgresql.org, greg@2ndquadrant.com, shortcutter@googlemail.com, tv@fuzzy.cz Subject: Re: Long Running Update - My Solution References: <4E035649.20007@riskcontrollimited.com> <4E089B6A.4000208@riskcontrollimited.com> <4E085789020000250003EC5E@gw.wicourts.gov> In-Reply-To: <4E085789020000250003EC5E@gw.wicourts.gov> X-TagToolbar-Keys: D20110628104857688 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 28 Jun 2011 09:48:50.0469 (UTC) FILETIME=[9555FD50:01CC3578] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201106/258 X-Sequence-Number: 44191 Hello Kevin > If you use EXPLAIN with both statements... Yes, the plans are indeed very different. Here is the statement, set to update up to 100,000 records, which took about 5 seconds to complete: UPDATE table_A SET field_1 = table_B.field_1 , field_2 = table_B.field_2 FROM table_B WHERE table_B.tb_id >= 0 AND table_B.tb_id <= 100000 AND table_B.tb_id = table_A.ta_id ; The query plan for the above is: Nested Loop (cost=0.00..2127044.47 rows=73620 width=63) -> Index Scan using table_B_pkey on table_B (cost=0.00..151830.75 rows=73620 width=20) Index Cond: ((tb_id >= 0) AND (tb_id <= 100000)) -> Index Scan using table_A_pkey on table_A (cost=0.00..26.82 rows=1 width=47) Index Cond: (table_A.ta_id = table_B.tb_id) Now, if I change the first AND clause to update 1M records, as follows: table_B.id <= 1000000 I get the following - quite different - query plan: Hash Join (cost=537057.49..8041177.88 rows=852150 width=63) Hash Cond: (table_A.ta_id = table_B.tb_id) -> Seq Scan on table_A (cost=0.00..3294347.71 rows=145561171 width=47) -> Hash (cost=521411.62..521411.62 rows=852150 width=20) -> Bitmap Heap Scan on table_B (cost=22454.78..521411.62 rows=852150 width=20) Recheck Cond: ((tb_id >= 0) AND (tb_id <= 1000000)) -> Bitmap Index Scan on table_B_pkey (cost=0.00..22241.74 rows=852150 width=0) Index Cond: ((tb_id >= 0) AND (tb_id <= 1000000)) Note: When I tried updating 1M records, the command was still running after 25 minutes before I killed it. The sequential scan in the later plan looks expensive, and (I think) supports what others have since mentioned, namely that when the optimizer moves to using sequential scans (working off the disk) things get a lot slower. For me, the penny has finally dropped on why I should use EXPLAIN for bulk operations. Thanks too, to Greg Smith, Robert Klemme and Thomas for all the feedback. Kind regards Harry Mantheakis London, UK