X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2430AD86F1 for ; Mon, 12 Sep 2005 10:33:59 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 27381-07 for ; Mon, 12 Sep 2005 13:33:53 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 4B35DD85CA for ; Mon, 12 Sep 2005 10:33:54 -0300 (ADT) Received: (qmail 22741 invoked by uid 500); 12 Sep 2005 13:34:11 -0000 Date: Mon, 12 Sep 2005 08:34:11 -0500 From: Bruno Wolff III To: Hilary Forbes Cc: pgsql-performance@postgresql.org Subject: Re: Slow update Message-ID: <20050912133411.GA21730@wolff.to> Mail-Followup-To: Hilary Forbes , pgsql-performance@postgresql.org References: <5.1.0.14.0.20050912100150.01f47f18@mailserver.dmr.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5.1.0.14.0.20050912100150.01f47f18@mailserver.dmr.co.uk> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200509/142 X-Sequence-Number: 14450 On Mon, Sep 12, 2005 at 10:14:25 +0100, Hilary Forbes wrote: > Hello everyone > > I must be doing something very wrong here so help please! I have two tables > > tableA has 300,000 recs > tableB has 20,000 recs > > I need to set the value of a field in table A to a value in table B depending on the existence of the record in table B. So what I have done is > > UPDATE tableA set a.val1=b.somefield FROM tableA a, tableB b WHERE a.key1=b.key1; > > The primary key of tableA is key1 and that of tableB is key1 ie the join is on primary keys. > > The "optimizer" has elected to d a sequential scan on tableA to determine which fields to update rather than the query being driveb by tableB and it is taking forever. Surely I must be able to force the system to read down tableB in preference to reading down tableA? It would help to see the exact query and the explain analyze output. Hopefully you didn't really write the query similar to above, since it is using illegal syntax and the if it was changed slightly to become legal than it would do a cross join of table A with the inner join of tableA and tableB, which isn't what you want.