Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 7B7FA9FA43C for ; Wed, 31 Jan 2007 11:16:54 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 10065-06 for ; Wed, 31 Jan 2007 11:16:45 -0400 (AST) 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 E329D9FA26D for ; Wed, 31 Jan 2007 11:16:50 -0400 (AST) 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 l0VFFBT9025905; Wed, 31 Jan 2007 10:15:11 -0500 (EST) To: Gregory Stark cc: Alvaro Herrera , Luke Lonergan , Chad Wagner , Simon Riggs , Igor Lobanov , Richard Huxton , pgsql-performance@postgresql.org Subject: Re: Querying distinct values from a large table In-reply-to: <877iv3miqu.fsf@stark.xeocode.com> References: <87d54wmpvd.fsf@stark.xeocode.com> <20070130170443.GJ2303@alvh.no-ip.org> <16278.1170222919@sss.pgh.pa.us> <877iv3miqu.fsf@stark.xeocode.com> Comments: In-reply-to Gregory Stark message dated "31 Jan 2007 08:24:41 -0500" Date: Wed, 31 Jan 2007 10:15:10 -0500 Message-ID: <25904.1170256510@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/449 X-Sequence-Number: 23050 Gregory Stark writes: > On the other there are some common situations where you could see > atypical increases. Consider joining a bunch of small tables to > generate a large result set. The small tables are probably all in > memory and the result set may only have a small number of distinct > values. If you throw out the duplicates early you save *all* the > I/O. If you have to do a disk sort it could be many orders slower. Right, we already have support for doing that well, in the form of hashed aggregation. What needs to happen is to get that to work for DISTINCT as well as GROUP BY. IIRC, DISTINCT is currently rather thoroughly intertwined with ORDER BY, and we'd have to figure out some way to decouple them --- without breaking DISTINCT ON, which makes it a lot harder :-( regards, tom lane