Re: Separating data sets in a table

From: "OU" <moebius444(at)hotmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Separating data sets in a table
Date: 2002-08-24 21:52:23
Message-ID: ak8sqe$1lbh$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


"Andreas Tille" <tillea(at)rki(dot)de> a crit dans le message de news:
Pine(dot)LNX(dot)4(dot)44(dot)0208221126490(dot)1478-100000(at)wr-linux02(dot)rki(dot)ivbb(dot)bund(dot)de(dot)(dot)(dot)

...
> I tried to do the following approach:
>
> CREATE LOCAL TEMPORARY TABLE ImportOK () INHERITS (Import) ;
>
> INSERT INTO ImportOK SELECT * FROM Import i
> INNER JOIN Ref r ON i.Id = r.Id;
>
> DELETE FROM Import WHERE Id IN (SELECT Id FROM ImportOK) ;
...

> Unfortunately the latest statement is so terribly slow that I can't
> imagine that there is a better way to do this.
>

You must use EXISTS if you work with big tables.
EXISTS use indexes, and IN use temporary tables.

-- this change nothing for IN :
CREATE INDEX import_id_index ON import(id);
CREATE INDEX import_ok_id_index ON import_ok(id);
-- slow :
-- DELETE FROM import WHERE id IN (SELECT id FROM import_ok) ;
DELETE FROM import WHERE EXISTS (
SELECT id FROM import_ok AS ok
WHERE ok.id = import.id
);

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Stosberg 2002-08-24 23:27:28 Re: Separating data sets in a table
Previous Message OU 2002-08-24 19:23:46 Re: SELECT ... WHERE ... NOT IN (SELECT ...);