Re: Query runs very slowly in Postgres, but very fast in other DBMS

From: "Andrus" <noeetasoftspam(at)online(dot)ee>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Query runs very slowly in Postgres, but very fast in other DBMS
Date: 2005-04-12 10:09:42
Message-ID: d3g6pg$nik$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>> SELECT dokumnr FROM rid WHERE dokumnr NOT IN
>> (select dokumnr FROM dok);
> ...
>> Is it possible to speed up this query is Postgres ? How to force Postgres
>> to use indexes for this query ?
>
> Use IN and NOT IN only for small sets. Use JOIN (instead of IN) and LEFT
> JOIN (instead of NOT IN) for larger sets. e.g.:
>
> SELECT rid.dokumnr
> FROM rid
> LEFT JOIN dok ON (dok.dokumnr = rid.dokumnr)
> WHERE dok.dokumnr iS NULL;

Thank you.
How to use this technique to speed up the update statement

UPDATE rid SET dokumnr=NULL WHERE
dokumnr NOT IN (SELECT dokumnr FROM dok);

and DELETE statement

DELETE FROM rid WHERE dokumnr NOT IN (SELECT dokumnr FROM dok);

Andrus

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ilya A. Kovalenko 2005-04-12 12:34:04 operating "inet" type
Previous Message Oleg Bartunov 2005-04-12 07:30:51 Re: OpenFTS