Re: Finding rows in table T1 that DO NOT MATCH any row in table T2 - Mailing list pgsql-performance

From Tom Lane
Subject Re: Finding rows in table T1 that DO NOT MATCH any row in table T2
Date
Msg-id 26129.1256047147@sss.pgh.pa.us
Whole thread Raw
In response to Finding rows in table T1 that DO NOT MATCH any row in table T2  (Shaul Dar <shauldar@gmail.com>)
Responses Re: Finding rows in table T1 that DO NOT MATCH any row in table T2  (Melton Low <softw.db@gmail.com>)
Re: Finding rows in table T1 that DO NOT MATCH any row in table T2  (Shaul Dar <shauldar@gmail.com>)
List pgsql-performance
Shaul Dar <shauldar@gmail.com> writes:
> I assume this will work but will take a long time:

> DELETE * FROM T1 where T1.PK NOT IN
> (SELECT T1.PK FROM T1, T2 where T1.PK = T2.FK)

Well, yeah, but it's unnecessarily inefficient --- why not just

DELETE FROM T1 where T1.PK NOT IN
(SELECT T2.FK FROM T2)

However, that still won't be tremendously fast unless the subselect fits
in work_mem.  As of 8.4 this variant should be reasonable:

DELETE FROM T1 where NOT EXISTS
(SELECT 1 FROM T2 where T1.PK = T2.FK)

Pre-8.4 you should resort to the "left join where is null" trick,
but there's no need to be so obscure as of 8.4.

            regards, tom lane

pgsql-performance by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: Finding rows in table T1 that DO NOT MATCH any row in table T2
Next
From: Melton Low
Date:
Subject: Re: Finding rows in table T1 that DO NOT MATCH any row in table T2