Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id D9E419FA37A for ; Wed, 6 Dec 2006 16:29:49 -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 48395-05 for ; Wed, 6 Dec 2006 16:29:40 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from pat.uio.no (pat.uio.no [129.240.10.15]) by postgresql.org (Postfix) with ESMTP id 690DD9FA339 for ; Wed, 6 Dec 2006 16:29:43 -0400 (AST) Received: from mail-mx3.uio.no ([129.240.10.44]) by pat.uio.no with esmtp (Exim 4.43) id 1Gs3Oc-00035z-Qc; Wed, 06 Dec 2006 21:29:38 +0100 Received: from ti200710a080-2739.bb.online.no ([85.164.138.179] helo=[10.1.1.101]) by mail-mx3.uio.no with esmtpsa (SSLv3:RC4-MD5:128) (Exim 4.43) id 1Gs3OZ-0004kt-G3; Wed, 06 Dec 2006 21:29:35 +0100 Subject: Re: Problems with an update-from statement and pg-8.1.4 From: Rafael Martinez To: Erik Jones Cc: Ted Allen , Stephan Szabo , pgsql-performance@postgresql.org In-Reply-To: <457725DA.90402@myemma.com> References: <1165432244.9606.18.camel@bbking.linux> <20061206111630.R8187@megazone.bigpanda.com> <45772022.4050303@blackducksoftware.com> <1165435845.9606.26.camel@bbking.linux> <457725DA.90402@myemma.com> Content-Type: text/plain Organization: University of Oslo Date: Wed, 06 Dec 2006 21:29:26 +0100 Message-Id: <1165436967.9606.32.camel@bbking.linux> Mime-Version: 1.0 X-Mailer: Evolution 2.6.0 Content-Transfer-Encoding: 7bit X-UiO-Spam-info: not spam, SpamAssassin (score=-3.974, required 12, autolearn=disabled, AWL 0.89, RCVD_IN_SORBS_DUL 0.14, UIO_MAIL_IS_INTERNAL -5.00) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.026 tagged_above=0 required=5 tests=AWL, BAYES_50 X-Spam-Level: X-Archive-Number: 200612/149 X-Sequence-Number: 22205 On Wed, 2006-12-06 at 14:19 -0600, Erik Jones wrote: > Rafael Martinez wrote: > > On Wed, 2006-12-06 at 14:55 -0500, Ted Allen wrote: > > > >> Stephan Szabo wrote: > >> > >>> On Wed, 6 Dec 2006, Rafael Martinez wrote: > >>> > >>>> mailstats=# EXPLAIN update mail SET spamscore = '-5.026' FROM mail m, > >>>> mail_received mr where mr.mail_id = m.mail_id AND mr.queue_id = > >>>> '1GrxLs-0004N9-I1' and mr.mailhost = '129.240.10.47'; > >>>> > >>>> > >>> I don't think this statement does what you expect. You're ending up with > >>> two copies of mail in the above one as "mail" and one as "m". You probably > >>> want to remove the mail m in FROM and use mail rather than m in the > >>> where clause. > >>> > >>> > >>> > >> Worse yet I think your setting "spamcore" for EVERY row in mail to > >> '-5.026'. The above solution should fix it though. > >> > >> -- Ted > >> > >> > > > > Thanks for the answers. I think the 'problem' is explain in the > > documentation: > > > > "fromlist > > > > A list of table expressions, allowing columns from other tables to > > appear in the WHERE condition and the update expressions. This is > > similar to the list of tables that can be specified in the FROMClause of > > a SELECT statement. Note that the target table must not appear in the > > fromlist, unless you intend a self-join (in which case it must appear > > with an alias in the fromlist)". > > > > And as you said, we can not have 'mail m' in the FROM clause. I have > > contacted the developers and they will change the statement. I gave then > > these 2 examples: > > > > ------------------------------------------------------------------------------- > > mailstats=# EXPLAIN update mail SET spamscore = '-5.026' FROM > > mail_received mr where mr.mail_id = mail.mail_id AND mr.queue_id = > > '1GrxLs-0004N9-I1' and mr.mailhost = '129.240.10.47'; > > QUERY PLAN > > ------------------------------------------------------------------------------------------------------ > > Nested Loop (cost=0.00..6.54 rows=1 width=57) > > -> Index Scan using received_queue_id_index on mail_received mr > > (cost=0.00..3.20 rows=1 width=4) > > Index Cond: ((queue_id)::text = '1GrxLs-0004N9-I1'::text) > > Filter: (mailhost = '129.240.10.47'::inet) > > -> Index Scan using mail_pkey on mail (cost=0.00..3.32 rows=1 > > width=57) > > Index Cond: ("outer".mail_id = mail.mail_id) > > (6 rows) > > > > mailstats=# explain update mail SET spamscore = '-5.026' where mail_id > > = (select mail_id from mail_received where queue_id = '1GrxLs-0004N9-I1' > > and mailhost = '129.240.10.47'); > > QUERY PLAN > > ----------------------------------------------------------------------------------------------------- > > Index Scan using mail_pkey on mail (cost=3.20..6.52 rows=1 width=57) > > Index Cond: (mail_id = $0) > > InitPlan > > -> Index Scan using received_queue_id_index on mail_received > > (cost=0.00..3.20 rows=1 width=4) > > Index Cond: ((queue_id)::text = '1GrxLs-0004N9-I1'::text) > > Filter: (mailhost = '129.240.10.47'::inet) > > (6 rows) > > ------------------------------------------------------------------------------- > > > Look again at the estimated costs of those two query plans. You haven't > gained anything there. Try this out: > > EXPLAIN UPDATE mail > SET spamscore = '-5.026' > FROM mail_received mr > WHERE mail.mail_id = mr.mail_id AND mr.queue_id = '1GrxLs-0004N9-I1' ; > Haven't we? * In the statement with problems we got this: Nested Loop (cost=0.00..932360.78 rows=7184312 width=57) * In the ones I sent: Nested Loop (cost=0.00..6.54 rows=1 width=57) Index Scan using mail_pkey on mail (cost=3.20..6.52 rows=1 width=57) * And in the last one you sent me: ------------------------------------------------------ Nested Loop (cost=0.00..6.53 rows=1 width=57) -> Index Scan using received_queue_id_index on mail_received mr (cost=0.00..3.20 rows=1 width=4) Index Cond: ((queue_id)::text = '1GrxLs-0004N9-I1'::text) -> Index Scan using mail_pkey on mail (cost=0.00..3.32 rows=1 width=57) Index Cond: (mail.mail_id = "outer".mail_id) (5 rows) ------------------------------------------------------ I can not see the different. regards, -- Rafael Martinez, Center for Information Technology Services University of Oslo, Norway PGP Public Key: http://folk.uio.no/rafael/