Re: PostgreSQL seems to create inefficient plans in simple conditional joins

Поиск
Список
Период
Сортировка
От Hedayat Vatankhah
Тема Re: PostgreSQL seems to create inefficient plans in simple conditional joins
Дата
Msg-id 56B856E9.8070703@gmail.com
обсуждение исходный текст
Ответ на Re: PostgreSQL seems to create inefficient plans in simple conditional joins  (Hedayat Vatankhah <hedayat.fwd@gmail.com>)
Список pgsql-performance
Hi again,

/*Hedayat Vatankhah*/ wrote on Sun, 31 Jan 2016 01:20:53 +0330:
> Hi,
>
> /*David Rowley*/ wrote on Sun, 31 Jan 2016 04:57:04 +1300:
>> On 31 January 2016 at 01:30, Hedayat Vatankhah
>> <hedayat.fwd@gmail.com> wrote:
>>> Personally, I expect both queries below to perform exactly the same:
>>>
>>> SELECT
>>>      t1.id, *
>>> FROM
>>>      t1
>>> INNER JOIN
>>>      t2 ON t1.id = t2.id
>>>      where t1.id > -9223372036513411363;
>>>
>>> And:
>>>
>>> SELECT
>>>      t1.id, *
>>> FROM
>>>      t1
>>> INNER JOIN
>>>      t2 ON t1.id = t2.id
>>>      where t1.id > -9223372036513411363 and t2.id >
>>> -9223372036513411363;
>>>
>>> Unfortunately, they do not. PostgreSQL creates different plans for
>>> these
>>> queries, which results in very poor performance for the first one
>>> compared
>>> to the second (What I'm testing against is a DB with around 350 million
>>> rows in t1, and slightly less in t2).
>>>
>>> EXPLAIN output:
>>> First query: http://explain.depesz.com/s/uauk
>>> Second query: link: http://explain.depesz.com/s/uQd
>> Yes, unfortunately you've done about the only thing that you can do,
>> and that's just include both conditions in the query. Is there some
>> special reason why you can't just write the t2.id > ... condition in
>> the query too? or is the query generated dynamically by some software
>> that you have no control over?

I just found another issue with using a query like the second one (using
LEFT JOINs instead of INNER JOINs): referencing id columns of joined
tables explicitly disables PostgreSQL join removal optimization when you
only select column(s) from t1! :(
I should forget about creating views on top of JOIN queries, and build
appropriate JOIN queries with referenced table and appropriate
conditions manually, so the whole data model should be exposed to the
application.

If I'm not wrong, PostgreSQL should understand that ANY condition on t2
doesn't change the LEFT JOIN output when t2 columns are not SELECTed.

Regards,
Hedayat



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

Предыдущее
От: Filip Rembiałkowski
Дата:
Сообщение: Re: bad COPY performance with NOTIFY in a trigger
Следующее
От: Gustav Karlsson
Дата:
Сообщение: Primary key index suddenly became very slow