Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 309A19FB367 for ; Sat, 7 Apr 2007 13:39:43 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 81318-08 for ; Sat, 7 Apr 2007 13:39:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 11A5B9FB352 for ; Sat, 7 Apr 2007 13:39:39 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id l37Gdc1F009916; Sat, 7 Apr 2007 12:39:38 -0400 (EDT) To: Tilo Buschmann cc: Arjen van der Meijden , PostgreSQL Performance Subject: Re: fast DISTINCT or EXIST In-reply-to: <20070407182407.297e7e35@wonderland> References: <20070407124713.1d4efe63@wonderland> <46178F65.5090807@tweakers.net> <9293.1175961248@sss.pgh.pa.us> <20070407182407.297e7e35@wonderland> Comments: In-reply-to Tilo Buschmann message dated "Sat, 07 Apr 2007 18:24:07 +0200" Date: Sat, 07 Apr 2007 12:39:38 -0400 Message-ID: <9915.1175963978@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/194 X-Sequence-Number: 24071 Tilo Buschmann writes: >> Arjen van der Meijden writes: >>> SELECT ... FROM cd >>> JOIN tracks ... >>> WHERE cd.id IN (SELECT DISTINCT cd_id FROM (SELECT t.cd_id FROM tracks t >>> WHERE t.tstitle @@ plainto_tsquery('simple','education') LIMIT 30) >>> as foo LIMIT 10) > Unfortunately, the query above will definitely not work correctly, if > someone searches for "a" or "the". Well, the "incorrectness" is only that it might deliver fewer than the hoped-for ten CDs ... but that was a completely arbitrary cutoff anyway, no? I think in practice this'd give perfectly acceptable results. > Actually, I hoped to find an alternative, that does not involve > DISTINCT. You could try playing around with GROUP BY rather than DISTINCT; those are separate code paths and will probably give you different plans. But I don't think you'll find that GROUP BY does any better on this particular measure of yielding rows before the full input has been scanned. regards, tom lane