Re: contracting tables - Mailing list pgsql-sql

From merlyn@stonehenge.com (Randal L. Schwartz)
Subject Re: contracting tables
Date
Msg-id m1lmgow6v1.fsf@halfdome.holdit.com
Whole thread Raw
In response to Re: contracting tables  (Jeff Eckermann <jeff_eckermann@yahoo.com>)
List pgsql-sql
>>>>> "Jeff" == Jeff Eckermann <jeff_eckermann@yahoo.com> writes:

Jeff> If you don't have a lot of indexes, sequences,
Jeff> referential integrity constraints etc. the easiest way
Jeff> would be:
Jeff> SELECT INTO table2 DISTINCT * FROM table1;
Jeff> DROP table1;
Jeff> ALTER TABLE table2 RENAME TO table1;
Jeff> Then recreate your other objects/constraints.

Jeff> If you want to do it in place, then:
Jeff> DELETE FROM table1 
Jeff> WHERE EXISTS (
Jeff>      SELECT * FROM table1 AS t1 
Jeff>      WHERE t1.key < table1.key
Jeff> );
Jeff> You will need an index on your "key" value, or this
Jeff> will take a long time on a large table.

Or maybe something like:

DELETE FROM table1
WHERE oid NOT IN (SELECT min(oid) FROM table1 GROUP BY key1, key2)

Untested, and I might be a little fuzzy on the syntax.  This keeps
the lowest oid row for the given key1/key2 pair.  Change that to *
to remove duplicates across all columns.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


pgsql-sql by date:

Previous
From: "Duncan Adams (DNS)"
Date:
Subject: Re: [NOVICE] Logging
Next
From: "Aasmund Midttun Godal"
Date:
Subject: Re: PL/pgSQL examples NOT involving functions