Skip site navigation (1) Skip section navigation (2)

Re: correlated delete with 'in' and 'left outer join'

From: <mike(at)linkify(dot)com>
To: <mascarm(at)mascari(dot)com>
Cc: <sszabo(at)megazone(dot)bigpanda(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: correlated delete with 'in' and 'left outer join'
Date: 2004-02-27 06:50:57
Message-ID: 49883.12.103.245.130.1077864658.squirrel@mail.linkify.com (view raw or flat)
Thread:
Lists: pgsql-general
The subquery will always return a row from LogEvent, but that row's itemID
will be null if theitemID doesn't match a row from Item.  That's why the subquery has the
"and i.ItemID is null".

> Stephan Szabo wrote:
>
>> On Thu, 26 Feb 2004 mike(at)linkify(dot)com wrote:
>>
>>
>>>I'm using postgresl 7.3.2 and have a query that executes very slowly.
>>>
>>>There are 2 tables: Item and LogEvent.  ItemID (an int4) is the
>>>primary key of Item, and is also a field in LogEvent.  Some ItemIDs in
>>>LogEvent do not correspond to ItemIDs in Item, and periodically we
>>>need to purge the non-matching ItemIDs from LogEvent.
>>>
>>>The query is:
>>>
>>>delete from LogEvent where EventType != 'i' and ItemID in
>>>(select distinct e.ItemID from LogEvent e left outer join Item i on
>>>e.ItemID = i.ItemID where e.EventType != 'i' and i.ItemID is null);
>>>
>>>I understand that using "in" is not very efficient.
>>>
>>>Is there some other way to write this query without the "in"?
>>
>>
>> Perhaps
>> delete from LogEvent where EventType != 'i' and not exists
>>  (select * from Item i where i.ItemID=LogEvent.ItemID);
>
> Maybe I'm not reading his subquery correctly, but the left outer
> join will produce a row from LogEvent regardless of whether or not a
> matching row exists in Item, correct? So doesn't it reduce to:
>
> DELETE FROM LogEvent WHERE EventType <> 'i';
>
> ???
>
> Mike Mascari



In response to

Responses

pgsql-general by date

Next:From: Mike MascariDate: 2004-02-27 07:07:22
Subject: Re: correlated delete with 'in' and 'left outer join'
Previous:From: Mike MascariDate: 2004-02-27 06:49:30
Subject: Re: correlated delete with "in" and "left outer join"

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group