Re: Query runs very slowly in Postgres, but very fast in other DBMS - Mailing list pgsql-sql

From Ezequiel Tolnay
Subject Re: Query runs very slowly in Postgres, but very fast in other DBMS
Date
Msg-id 425B5E66.8080905@etolnay.com.ar
Whole thread Raw
In response to Query runs very slowly in Postgres, but very fast in other DBMS  ("Andrus Moor" <eetasoft@online.ee>)
List pgsql-sql
Andrus Moor wrote:
> 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;

Cheers,

Ezequiel Tolnay


pgsql-sql by date:

Previous
From: "Ilya A. Kovalenko"
Date:
Subject: operating "inet" type
Next
From: "Bill Lawrence"
Date:
Subject: Re: Getting the output of a function used in a where clause