pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ezequiel Tolnay <mail@etolnay.com.ar>
To: Andrus Moor <eetasoft@online.ee>
Subject: Re: Query runs very slowly in Postgres, but very fast in other DBMS
Date: Tue, 12 Apr 2005 15:36:38 +1000
Message-ID: <425B5E66.8080905@etolnay.com.ar> (raw)
In-Reply-To: <d3er3k$2m2d$1@news.hub.org>
References: <d3er3k$2m2d$1@news.hub.org>

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



view thread (6+ messages)  latest in thread

Message-ID: <425B5E66.8080905@etolnay.com.ar>
Permalink:  ../425B5E66.8080905@etolnay.com.ar/
Also on:    postgresql.org/message-id/425B5E66.8080905@etolnay.com.ar

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@etolnay.com.ar, eetasoft@online.ee
  Subject: Re: Query runs very slowly in Postgres, but very fast in other DBMS
  In-Reply-To: <425B5E66.8080905@etolnay.com.ar>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox