Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id DDCE21337B52 for ; Wed, 29 Sep 2010 08:58:09 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 63952-07 for ; Wed, 29 Sep 2010 11:58:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from zebedee.cranntara.net (zebedee.cranntara.net [91.186.7.79]) by mail.postgresql.org (Postfix) with ESMTP id A0BA41337B34 for ; Wed, 29 Sep 2010 08:58:00 -0300 (ADT) Received: from [88.210.158.208] (helo=bolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1P0vIM-0005rV-EH for pgsql-sql@postgresql.org; Wed, 29 Sep 2010 12:58:00 +0100 Received: from [192.168.0.32] (helo=dick.coachhouse ident=tarlika) by bolek.coachhouse with esmtp (Exim 4.69) (envelope-from ) id 1P0v24-0003jB-7b for pgsql-sql@postgresql.org; Wed, 29 Sep 2010 12:41:08 +0100 Date: Wed, 29 Sep 2010 12:41:07 +0100 From: Tarlika Elisabeth Schmitz To: pgsql-sql@postgresql.org Subject: Re: identifying duplicates in table with redundancies Message-ID: <20100929124107.245b1314@dick.coachhouse> In-Reply-To: <4CA2FB63.2030102@longimanus.net> References: <20100923223940.5019475e@dick.coachhouse> <8590CAE9E5504E23AB08C4EF31EA066F@marktestcr.marktest.pt> <20100924171226.6000b310@dick.coachhouse> <065CBEA1-1647-4233-BBB6-48E879FD91B0@gmail.com> <20100927175418.162ee906@dick.coachhouse> <5F1C28C4D61848D6A012C5DD84B22226@marktestcr.marktest.pt> <20100928213600.4db60ba4@dick.coachhouse> <4CA2FB63.2030102@longimanus.net> X-Mailer: Claws Mail 3.5.0 (GTK+ 2.12.12; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201009/115 X-Sequence-Number: 34646 On Wed, 29 Sep 2010 10:40:03 +0200 Andreas Schmitz wrote: > On 09/28/2010 10:36 PM, Tarlika Elisabeth Schmitz wrote: >> On Tue, 28 Sep 2010 11:34:31 +0100 >> "Oliveiros d'Azevedo Cristina" >> wrote: >> >>> ----- Original Message ----- >>> From: "Tarlika Elisabeth Schmitz" >>> To: >>> Sent: Monday, September 27, 2010 5:54 PM >>> Subject: Re: [SQL] identifying duplicates in table with redundancies >>> >>> >>>> On Fri, 24 Sep 2010 18:12:18 +0100 >>>> Oliver d'Azevedo Christina wrote: >>>> >>>>>>> SELECT DISTINCT trainer_id,trainer_name >>>>>>> FROM ( >>>>>>> SELECT trainer_name -- The field you want to test for >>>>>>> duplicates FROM ( >>>>>>> SELECT DISTINCT "trainer_id","trainer_name" >>>>>>> FROM student >>>>>>> ) x >>>>>>> GROUP BY "trainer_name" -- the field you want to test for >>>>>>> duplicates >>>>>>> HAVING (COUNT(*)> 1) >>>>>>> ) z >>>>>>> NATURAL JOIN student y >>>> >>>> >>>> What indices would you recommend for this operation? >>> But, on this query in particular I would recomend an indice on >>> trainer_name,[...] >> Strangely, these indices did not do anything. >> Without, the query took about 8500ms. Same with index. >> >> The table has 250000 records. 11000 have trainer_name = null. Only >> 13000 unique trainer_names. > >I guess explain analyze shows up a seq scan. try avoiding to use >distinct. use group by instead. > >regards, Andreas Hallo Andreas, I reduced the problem to the innermost query: 1) SELECT DISTINCT trainer_id, trainer_name FROM student This results in a sequential table scan. Execution time 7500ms. 2) I created an INDEX ON (trainer_id, trainer_name). Then I had an index scan instead, which still cost 7000ms. 3) Next, I changed from DISTINCT to GROUP BY: SELECT trainer_id, trainer_name FROM student GROUP BY trainer_id, trainer_name This resulted in an index scan @ 6750ms 4) I filtered out NULL trainer_ids WHERE trainer_id IS NOT NULL Amazingly, this resulted in a sequential table scan, which only took 1300ms!! Please, explain (pun not intended)! How can this be. Only 11000/250000 rows have a null trainer_id. Thanks for the GROUP BY tip! -- Best Regards, Tarlika Elisabeth Schmitz