X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4D682D1B52C for ; Thu, 28 Aug 2003 01:07:47 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 89135-09 for ; Wed, 27 Aug 2003 22:07:19 -0300 (ADT) Received: from omnet.leaderofthedot.com (unknown [65.210.122.2]) by svr1.postgresql.org (Postfix) with ESMTP id C1C41D1B8AE for ; Wed, 27 Aug 2003 22:07:13 -0300 (ADT) Received: from Office3 (pcp01840738pcs.owngsm01.md.comcast.net [68.32.121.151]) by omnet.leaderofthedot.com (8.12.9/8.12.9) with SMTP id h7S0x1p5006369; Wed, 27 Aug 2003 20:59:03 -0400 (EDT) From: "ProgHome" To: "'Stephan Szabo'" Cc: "'Franco Bruno Borghesi'" , Subject: Re: How to optimize this query ? Date: Wed, 27 Aug 2003 21:01:27 -0400 Message-ID: <037f01c36cff$ec09c770$0700a8c0@Office3> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2616 In-Reply-To: <20030813112311.J53899-100000@megazone.bigpanda.com> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Importance: Normal X-Scanned-By: MIMEDefang 2.36 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/338 X-Sequence-Number: 14954 You were right, Stephan ! The query below is still not correct ... because the second line shouldn't be shown ! Now I really don't know how I could rewrite this without a subquery because it doesn't seem to be possible with some LEFT or INNER joins ! Do you have an idea ? -----Original Message----- From: Stephan Szabo [mailto:sszabo@megazone.bigpanda.com] Sent: Wednesday, August 13, 2003 2:29 PM To: ProgHome Cc: 'Franco Bruno Borghesi'; pgsql-sql@postgresql.org Subject: RE: [SQL] How to optimize this query ? On Wed, 13 Aug 2003, ProgHome wrote: > I tried with some LEFT JOINS, which give me the possibility to keep > the information of the right table. > > I have now the following query, which is 10 times faster !!! (from 16s > to 1.6s) > But I'd like to remove the last subquery, to see if it faster ;) > > > ------------------------- > > SELECT lead. * > FROM lead > LEFT JOIN purchase ON ( lead.id = purchase.lead_id ) > LEFT JOIN affiliate_lockout ON ( lead.affiliate_id = > affiliate_lockout.affiliate_locked_id ) > WHERE ( > exclusive IS NULL OR ( > exclusive = 0 AND nb_purchases < 3 > ) > ) AND id NOT > IN ( > > SELECT lead_id > FROM purchase > INNER JOIN member_exclusion > WHERE purchase.member_id = member_exclusion.member_id_to_exclude AND > purchase.member_id = 21101 > ) AND ( > affiliate_lockout.member_id <> 21101 OR affiliate_lockout.member_id IS > NULL > ) AND purchase.member_id <> 21101 > GROUP BY lead.id As I replied to Franco for his query below, I believe this query is not equivalent to your original query for a few cases, but those might not come up. If you had a row in lead like id = 2, affiliate_id = 2 And rows in affiliate_lockout like: affiliate_locked_id=2, member_id=21101 affiliate_locked_id=2, member_id=31101 should this row in lead be shown or not? In the original query I think it would not (because lead.affiliate_id was IN the affiliate_lockout table where member_id=21101). In the above query I think it will, because one of the joined tables will have the lead information and a member_id that is not equal to 21101.