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

From: Ezequiel Tolnay <mail(at)etolnay(dot)com(dot)ar>
To: Andrus Moor <eetasoft(at)online(dot)ee>
Subject: Re: Query runs very slowly in Postgres, but very fast in other DBMS
Date: 2005-04-12 05:36:38
Message-ID: 425B5E66.8080905@etolnay.com.ar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-04-12 05:43:10 Re: Query runs very slowly in Postgres, but very fast in other DBMS
Previous Message Bill Lawrence 2005-04-12 01:25:10 Re: Getting the output of a function used in a where clause