Re: Deleting orphaned records to establish Ref Integrity

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: Deleting orphaned records to establish Ref Integrity
Дата
Msg-id 87psv4eixp.fsf@stark.xeocode.com
обсуждение исходный текст
Ответ на Re: Deleting orphaned records to establish Ref Integrity  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Tom Lane <tgl@sss.pgh.pa.us> writes:

> "Roman F" <romanf@fusemail.com> writes:
>
> > DELETE FROM child_table WHERE parentid NOT IN
> >   (SELECT parentid FROM parent_table)
>
> Another idea is to try an outer join:
>
>     SELECT child_table.parentid INTO tmp_table
>     FROM child_table LEFT JOIN parent_table
>          ON (child_table.parentid = parent_table.parentid)
>     WHERE parent_table.parentid IS NULL;

There's also

DELETE
  FROM child_table
 WHERE NOT EXISTS (select 1
                     from parent_table
                    where parent_id = child_table.parent_id
                  )


Which won't use anything as efficient as a hash join or merge join but will be
at least capable of using index lookups for something basically equivalent to
a nested loop.



--
greg

В списке pgsql-general по дате отправления:

Предыдущее
От: Russ Brown
Дата:
Сообщение: Re: writting a large store procedure
Следующее
От: Joachim Zobel
Дата:
Сообщение: Limits of SQL