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 458A8D1C4E6 for ; Wed, 13 Aug 2003 13:49:25 +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 16330-07 for ; Wed, 13 Aug 2003 10:47:53 -0300 (ADT) Received: from news.hub.org (unknown [64.117.224.194]) by svr1.postgresql.org (Postfix) with ESMTP id 6E055D1C4D6 for ; Wed, 13 Aug 2003 10:49:14 -0300 (ADT) Received: from news.hub.org (host-64-117-224-194.altec1.com [64.117.224.194] (may be forged)) by news.hub.org (8.12.9/8.12.9) with ESMTP id h7DDn5eq096846 for ; Wed, 13 Aug 2003 13:49:14 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h7DDLY2b088459 for pgsql-sql@postgresql.org; Wed, 13 Aug 2003 13:21:34 GMT From: proghome@silesky.com (krystoffff) X-Newsgroups: comp.databases.postgresql.sql Subject: Re: How to optimize this query ? Date: 13 Aug 2003 06:20:43 -0700 Organization: http://groups.google.com/ Lines: 33 Message-ID: <85898f7e.0308130520.5af10fd0@posting.google.com> References: <4848.200.59.66.253.1060736672.squirrel@webmail.akyasociados.com.ar> <20030812180544.L27665-100000@megazone.bigpanda.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-abuse@google.com To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/157 X-Sequence-Number: 14773 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's like to remove the last subquery, to see if it faster ;) Can somebody help me ? ------------------------- 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