Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 6069D9FA162 for ; Wed, 13 Dec 2006 09:21:02 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 70701-04 for ; Wed, 13 Dec 2006 09:20:43 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id B59389FA282 for ; Wed, 13 Dec 2006 09:20:48 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mx.inode.at (lb01nat04.inode.at [62.99.145.4]) by svr4.postgresql.org (Postfix) with ESMTP id C1B595AF8CC for ; Wed, 13 Dec 2006 13:20:47 +0000 (GMT) Received: from [83.64.182.18] (port=3446 helo=panettone.apus.local) by smartmx-04.inode.at with esmtp (Exim 4.50) id 1GuU2P-0007q0-Ny; Wed, 13 Dec 2006 14:20:45 +0100 Received: from xjens.apus.local ([193.1.1.148]) by panettone.apus.local with esmtp (Exim 3.35 #1 (Debian)) id 1GuU2P-0004pn-00; Wed, 13 Dec 2006 14:20:45 +0100 Date: Wed, 13 Dec 2006 14:20:40 +0100 To: "Arnaud Lesauvage" Subject: Re: Slow update with simple query From: "Jens Schipkowski" Organization: APUS Software GmbH Cc: pgsql-performance@postgresql.org Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <457FDB1E.6040801@freesurf.fr> <457FF0CD.4050606@freesurf.fr> Content-Transfer-Encoding: 8bit Message-ID: In-Reply-To: <457FF0CD.4050606@freesurf.fr> User-Agent: Opera Mail/9.02 (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/318 X-Sequence-Number: 22374 On Wed, 13 Dec 2006 13:23:41 +0100, Arnaud Lesauvage wrote: > Jens Schipkowski a écrit : >> the problem is a combination of bad formed SQL and maybe missing >> indexes. >> try this: >> UPDATE t1 >> SET booleanfield = foo.bar >> FROM (SELECT uid,(field IN ('some','other') AND field2 = 'Y') AS bar >> FROM t2) AS foo >> WHERE t1.uid=foo.uid; > > > Hi Jens, > Why is this query better than the other one ? Because it runs the > "(field IN ('some','other') AND field2 = 'Y')" once and then executes > the join with the resulting set ? True. The Subselect in FROM clause will be executed once and will be joined using the condition at where clause. So your condition at t2 is not executed for each row in t1(2mio records) but for each row in t2(1k records). And the boolean value is already set during update. regards, Jens > >> and index t1.uid, t2.uid, t2.field, t2.field2 > > t1.field can only take 3 or 4 values (don't remember exactly), and > field2 only 2 ('Y' or 'N'). So this fields have a very low cardinality. > Won't the planner chose to do a table scan in such a case ? > > Thanks for your advices ! > > -- > Arnaud -- ** APUS Software GmbH