Multiple-Table-Spanning Joins with ORs in WHERE Clause

Поиск
Список
Период
Сортировка
Искать
От
Sven R. Kunze
Тема
Multiple-Table-Spanning Joins with ORs in WHERE Clause
Дата
Msg-id
97dbd6f9-a289-94d1-325f-997df87c672a@mail.de
Список
Дерево обсуждения
Re: Multiple-Table-Spanning Joins with ORs in WHERE Clause Igor Neyman <ineyman@perceptron.com>
Re: Multiple-Table-Spanning Joins with ORs in WHERE Clause Igor Neyman <ineyman@perceptron.com>
Hi pgsql-performance list,


what is the recommended way of doing **multiple-table-spanning joins 
with ORs in the WHERE-clause**?


Until now, we've used the LEFT OUTER JOIN to filter big_table like so:


SELECT DISTINCT 
FROM
     "big_table"
     LEFT OUTER JOIN "table_a" ON ("big_table"."id" = 
"table_a"."big_table_id")
     LEFT OUTER JOIN "table_b" ON ("big_table"."id" = 
"table_b"."big_table_id")
WHERE
     "table_a"."item_id" IN ()
     OR
     "table_b"."item_id" IN ();


However, this results in an awful slow plan (requiring to scan the 
complete big_table which obviously isn't optimal).
So, we decided (at least for now) to split up the query into two 
separate ones and merge/de-duplicate the result with application logic:


SELECT 
FROM
     "big_table" INNER JOIN "table_a" ON ("big_table"."id" = 
"table_a"."big_table_id")
WHERE
     "table_a"."item_id" IN ();


SELECT 
FROM
     "big_table" INNER JOIN "table_b" ON ("big_table"."id" = 
"table_b"."big_table_id")
WHERE
     "table_b"."item_id" IN ();


As you can imagine we would be very glad to solve this issue with a 
single query and without having to re-code existing logic of PostgreSQL. 
But how?


Best,
Sven


PS: if you require EXPLAIN ANALYZE, I can post them as well.

В списке pgsql-performance по дате отправления
От: Knels, Udo
Дата:
От: Madusudanan.B.N
Дата:
FAQ