Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 256DD64FEE7 for ; Tue, 23 Dec 2008 15:47:53 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 47663-07 for ; Tue, 23 Dec 2008 15:47:50 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from cronos.madness.at (madness.at [217.196.146.217]) by mail.postgresql.org (Postfix) with ESMTP id 16EA364FE21 for ; Tue, 23 Dec 2008 15:47:49 -0400 (AST) Received: from mastermind.kaltenbrunner.cc ([83.215.233.60]) by cronos.madness.at with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1LFDEB-00057C-0A; Tue, 23 Dec 2008 20:47:41 +0100 Message-ID: <49514058.9090005@kaltenbrunner.cc> Date: Tue, 23 Dec 2008 20:47:36 +0100 From: Stefan Kaltenbrunner User-Agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018) MIME-Version: 1.0 To: Scott Marlowe CC: George Pavlov , Kynn Jones , pgsql-performance@postgresql.org Subject: Re: How to "unique-ify" HUGE table? References: <8C5B026B51B6854CBE88121DBF097A86033B2DED@ehost010-33.exch010.intermedia.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200812/207 X-Sequence-Number: 32243 Scott Marlowe wrote: > On Tue, Dec 23, 2008 at 11:14 AM, George Pavlov wrote: >> You don't say what PG version you are on, but just for kicks you may try >> using GROUP BY instead of DISTINCT. Yes, the two should perform the >> same, but with 8.1 (or maybe 8.0) I had seen situations where GROUP BY >> was faster (admittedly this happened with more complex queries). So, try >> this: > > Even in 8.3 it looks like group by is faster. Tested it on a decent > sized table and group by used a hash agg and ran in ~600 ms, while > distinct used a sort and ran in 1300 ms. That was on 500k rows. On a > much larger table, one with about 10M rows, a similar statement runs > in 1500 ms with group by and in 2390 ms when run with distinct. Not surprising - this is a known limitation in all released versions of postgresql (GROUP BY can use hashing and sorting - DISTINCT only sorting). 8.4 is going to improve that though. Stefan