Re: could not create unique index, table contains

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Christian Hofmann <christian(dot)hofmann(at)gmx(dot)de>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: could not create unique index, table contains
Date: 2005-11-27 22:24:38
Message-ID: 1133130278.1104.9.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Sun, 2005-11-27 at 22:45 +0100, Christian Hofmann wrote:
> Hello,
>
> I have a table with two columns (id, names).
>
> At this time there is no index, because i have not needed any one.
>
> There are about 120 million rows. Now I want to delete all duplicate rows.
> I thought I could just create a unique index and postgresql qould do the
> rest. But ok. Maybe postgresql didn't know which rows to delete.
>
> Is there a query that will delete all rows where the same name is more than
> one time in the table?
> I only want to have every name once in the table.
>
> My method to write a programm, select every row and cvompare it to the
> others would be very slow.

Do something like this:

SELECT DISTINCT * INTO new_table FROM old_table;
DROP TABLE old_table;
ALTER TABLE new_table RENAME TO old_table;

--
Oliver Elphick olly(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
GPG: 1024D/A54310EA 92C8 39E7 280E 3631 3F0E 1EC0 5664 7A2F A543 10EA
========================================
Do you want to know God? http://www.lfix.co.uk/knowing_god.html

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Announce 2005-11-27 23:23:16 How does PG Inheritance work?
Previous Message Christian Hofmann 2005-11-27 21:45:27 could not create unique index, table contains duplicated values