X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 7A7B2D1C98E for ; Thu, 29 Jan 2004 19:31:47 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 79852-02 for ; Thu, 29 Jan 2004 15:31:17 -0400 (AST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 6465DD1CCD7 for ; Thu, 29 Jan 2004 15:31:15 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i0TJVC31026582; Thu, 29 Jan 2004 14:31:12 -0500 (EST) To: Jack Coates Cc: pgsql-performance Subject: Re: query optimization question In-reply-to: <1075403095.7494.117.camel@cletus.lyris.com> References: <1075338353.7494.71.camel@cletus.lyris.com> <10115.1075341889@sss.pgh.pa.us> <1075396980.7494.107.camel@cletus.lyris.com> <25064.1075399504@sss.pgh.pa.us> <1075403095.7494.117.camel@cletus.lyris.com> Comments: In-reply-to Jack Coates message dated "Thu, 29 Jan 2004 11:04:55 -0800" Date: Thu, 29 Jan 2004 14:31:11 -0500 Message-ID: <26581.1075404671@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/308 X-Sequence-Number: 5482 Jack Coates writes: > jackdb=# explain SELECT DISTINCT members_.memberid_ > jackdb-# FROM members_ > jackdb-# WHERE ( members_.List_='list1' > jackdb(# AND members_.MemberType_='normal' > jackdb(# AND members_.SubType_='mail' > jackdb(# AND members_.emailaddr_ IS NOT NULL ) > jackdb-# GROUP BY memberid_ HAVING ( Um, that's not what I had in mind at all. Does GROUP BY actually do anything at all here? (You didn't answer me as to whether memberid_ is a unique identifier or not, but if it is, this GROUP BY is just an expensive no-op.) What I was envisioning was pulling the sub-selects up to the top level and using grouping to calculate the count(*) values for all memberids in parallel. Roughly speaking it would look like (again assuming memberid_ is unique) SELECT memberid_ FROM ( SELECT memberid_ FROM lyrActiveRecips, members_, outmail WHERE (all the conditions for this case) UNION ALL SELECT memberid_ FROM lyrCompletedRecips, members_, outmail WHERE (all the conditions for this case) ) GROUP BY memberid_ HAVING count(*) = 3; However, if you can't change the boilerplate part of your query then this is all blue-sky speculation anyway. What I'm actually more interested in is your statement that MSSQL can do the original query quickly. I find that a bit hard to believe because I don't see any relevant optimization techniques. Do they have any equivalent to EXPLAIN that would give some hint how they're doing it? regards, tom lane