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 8EBDBD1B56B for ; Wed, 13 Aug 2003 19:19:20 +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 59608-02 for ; Wed, 13 Aug 2003 16:17:44 -0300 (ADT) Received: from news.hub.org (unknown [64.117.224.194]) by svr1.postgresql.org (Postfix) with ESMTP id 54F24D1B524 for ; Wed, 13 Aug 2003 16:19:08 -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 h7DJJ8ek095360 for ; Wed, 13 Aug 2003 19:19:08 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h7DIskhb088769 for pgsql-sql@postgresql.org; Wed, 13 Aug 2003 18:54:46 GMT From: proghome@silesky.com (krystoffff) X-Newsgroups: comp.databases.postgresql.sql Subject: Re: How to optimize this query ? Date: 13 Aug 2003 11:54:16 -0700 Organization: http://groups.google.com/ Lines: 35 Message-ID: <85898f7e.0308131054.1b1801d2@posting.google.com> References: <4848.200.59.66.253.1060736672.squirrel@webmail.akyasociados.com.ar> <20030812180544.L27665-100000@megazone.bigpanda.com> <85898f7e.0308130520.5af10fd0@posting.google.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/150 X-Sequence-Number: 14766 OK, here is the final query without any subquery ... -------------------------- SELECT L. * FROM lead L LEFT JOIN purchase P ON ( L.id = P.lead_id ) LEFT JOIN member_exclusion M ON ( P.member_id = M.member_id_to_exclude ) LEFT JOIN ( SELECT DISTINCT affiliate_locked_id FROM affiliate_lockout WHERE member_id = 21101 ) AS A ON ( L.affiliate_id = A.affiliate_locked_id ) WHERE L.exclusive IS NULL OR ( L.exclusive = 0 AND L.nb_purchases < 3 ) AND ( P.lead_id IS NULL OR P.lead_id <> 21101 ) AND ( M.member_id IS NULL ) GROUP BY L.id ----------------------- I've got the same result as before, so it should be correct ;) By the way, the time seems to be the same (1.41s for the last form, and 1.44s now) but I think it's because I don't have much stuff in Member_exclusion (6 rows) so I will keep this query without subquery ... Thanks, Franco ! PS : definitively, try to avoid the subqueries ! It's easy to program, but very slow to execute !