pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Victor Ciurus <vikcious@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Simple machine-killing query!
Date: Thu, 21 Oct 2004 11:41:48 -0400
Message-ID: <16017.1098373308@sss.pgh.pa.us> (raw)
In-Reply-To: <e7a72f4004102107345063b467@mail.gmail.com>
References: <e7a72f4004102107345063b467@mail.gmail.com>

Victor Ciurus <vikcious@gmail.com> writes:
> What I am requested to do is to keep all records from 'BIGMA' that do
> not apear in 'DIRTY'
> So far I have tried solving this by going for:

> [explain] select * from BIGMA where string not in (select * from DIRTY);
>                                QUERY PLAN
> ------------------------------------------------------------------------
>  Seq Scan on bigma  (cost=0.00..24582291.25 rows=500 width=145)
>    Filter: (NOT (subplan))
>    SubPlan
>      ->  Seq Scan on dirty  (cost=0.00..42904.63 rows=2503963 width=82)
> (4 rows)

If you are using PG 7.4, you can get reasonable performance out of this
approach, but you need to jack sort_mem up to the point where the whole
DIRTY table will fit into sort_mem (so that you get a hashed-subplan
plan and not a plain subplan).  If you find yourself setting sort_mem to
more than say half of your machine's available RAM, you should probably
forget that idea.

> [explain] select * from bigma,dirty where bigma.email!=dirty.email;

This of course does not give the right answer at all.

A trick that people sometimes use is an outer join:

select * from bigma left join dirty on (bigma.email=dirty.email)
where dirty.email is null;

Understanding why this works is left as an exercise for the reader
... but it does work, and pretty well too.  If you're using pre-7.4
PG then this is about the only effective solution AFAIR.

			regards, tom lane



view thread (6+ messages)  latest in thread

Message-ID: <16017.1098373308@sss.pgh.pa.us>
Permalink:  ../16017.1098373308@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/16017.1098373308@sss.pgh.pa.us

 · 

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-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, vikcious@gmail.com
  Subject: Re: Simple machine-killing query!
  In-Reply-To: <16017.1098373308@sss.pgh.pa.us>

* 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