Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 4DDBC64FE8F for ; Tue, 23 Dec 2008 14:37:15 -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 35902-03 for ; Tue, 23 Dec 2008 14:37:08 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from rv-out-0506.google.com (rv-out-0506.google.com [209.85.198.224]) by mail.postgresql.org (Postfix) with ESMTP id 4E91964FE73 for ; Tue, 23 Dec 2008 14:37:08 -0400 (AST) Received: by rv-out-0506.google.com with SMTP id b25so3992010rvf.43 for ; Tue, 23 Dec 2008 10:37:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=74AL4UhUMkUx1dMKOFJIhGNKEVIoeB5MH7KT5vFC4Rs=; b=huVzWlV6nivVQwHN1w0yaZ/BpzLiv3c99ZrJUd4LMhybAnTupw/p+Dj0sue9hg4UZK aOtStp0DgHuK6dccuT8H05zobUqaB4DPmYrvhE/Am4svTM0XuyUiXxTyIADQpA3h/U1m /mOHsxzzNg2FU/2kccqfppBxERU2+TjMApxio= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=qcPPdZR7ILlJ+yiqZ6CiLXKkuL+jwKPRVYXZFPcA0G56Ez/PCFEkaNDUj5aj7ldwIz iH9oVaiw0kJqCokdXsTijxJCfBhs8N+aNkgePDuC4babxeew/qLbSBqdG0FHm4R6Ym/O 4bavCgv4bo19KpJ9B+IcFUsAZNSKK/eYHSlX0= Received: by 10.114.72.1 with SMTP id u1mr4974644waa.39.1230057427258; Tue, 23 Dec 2008 10:37:07 -0800 (PST) Received: by 10.114.132.17 with HTTP; Tue, 23 Dec 2008 10:37:07 -0800 (PST) Message-ID: Date: Tue, 23 Dec 2008 11:37:07 -0700 From: "Scott Marlowe" To: "George Pavlov" Subject: Re: How to "unique-ify" HUGE table? Cc: "Kynn Jones" , pgsql-performance@postgresql.org In-Reply-To: <8C5B026B51B6854CBE88121DBF097A86033B2DED@ehost010-33.exch010.intermedia.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <8C5B026B51B6854CBE88121DBF097A86033B2DED@ehost010-33.exch010.intermedia.net> 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/206 X-Sequence-Number: 32242 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.