From: pierre@desertmoon.com
To: herouth@oumail.openu.ac.il
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] MINUS and slow 'not in'
Date: 24 Nov 1998 07:57:56 -0700
Message-ID: <19981124145756.28759.qmail@desertmoon.com> (raw)
In-Reply-To: <l03110702b28025746cbe@[147.233.159.109]>
>
> At 6:53 +0200 on 24/11/98, pierre wrote:
>
> > I then tried using a 'not in' clause.
> >
> > select * from A where user_id not in (select * from B);
> >
> > This is VERY slow, and examining the explain output tells me that it will
> > use the user_id index for table B, but a sequential scan of A even though
> > A has an index for the user_id column.
>
> First, I assume you meant "select user_id from B", not "select *", or
> something is very strange here.
>
> You may try to convert the NOT IN to a NOT EXISTS clause, and see if it
> improves anything, but it will still require a sequential search.
>
> SELECT * FROM A
> WHERE NOT EXISTS (
> SELECT * FROM B
> WHERE B.user_id = A.user_id
> );
>
> By the way, if you have any specific criteria on A, besides the NOT EXISTS
> or NOT IN, they may cause an index scan on A as well.
>
Ok...remember that I have table A with 40k rows, and B with 2k. What I
want to really get out of the query are 2k rows from A that are not contained
in B. After reading your email, I thought about using a cursor and only
fetching the first 2k rows that match the query. This helped tremendously
in that it didn't try and return all 38k rows. However I now need to
take the results of the fetch and dump it into table B.
How can one use fetch to insert?
I've tried...
insert into B
fetch 2000 from fubar;
Which just gives a parser error. There is very little documentation on
cursors written up that I can find. I've even searched the email archives.
Ideas?
-=pierre
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: pierre@desertmoon.com, herouth@oumail.openu.ac.il
Subject: Re: [SQL] MINUS and slow 'not in'
In-Reply-To: <19981124145756.28759.qmail@desertmoon.com>
References: <l03110702b28025746cbe@[147.233.159.109]> <19981124145756.28759.qmail@desertmoon.com>
* 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