Re: --EXTERNAL--Re: PSQL does not remove obvious useless joins

Поиск
Список
Период
Сортировка
От Sfiligoi, Igor
Тема Re: --EXTERNAL--Re: PSQL does not remove obvious useless joins
Дата
Msg-id c66796bcbeaa4eef87948ad9e9d5101a@ASGEXCPWP06.ga.com
обсуждение исходный текст
Ответ на Re: PSQL does not remove obvious useless joins  (Merlin Moncure <mmoncure@gmail.com>)
Ответы Re: --EXTERNAL--Re: PSQL does not remove obvious useless joins  (Adrian Klaver <adrian.klaver@aklaver.com>)
Список pgsql-general
Sorry... the example was incomplete.

All the fields are defined as not-null.
So it is guaranteed to always match the join.

And PostgreSQL release notes claim that PGSQL can do at least partial join removal:
https://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.0#Join_Removal 

I was hoping this use case would fit in.

Any suggestions?

Igor

-----Original Message-----
From: Merlin Moncure [mailto:mmoncure@gmail.com] 
Sent: Friday, July 01, 2016 12:42 PM
To: Sfiligoi, Igor <Igor.Sfiligoi@ga.com>
Cc: pgsql-general@postgresql.org
Subject: --EXTERNAL--Re: [GENERAL] PSQL does not remove obvious useless joins

On Fri, Jul 1, 2016 at 12:17 PM, Sfiligoi, Igor <Igor.Sfiligoi@ga.com> wrote:
> Hello.
>
> We have a view that is very generic, and we noticed that PostgreSQL is 
> not very good at removing useless joins, which makes our queries very slow.
>
> We could change our code to avoid the view and write ad-hoc queries to 
> the underlying tables, but would prefer not to, if there is a way around it.
>
> (BTW: We are currently using psql 9.4)
>
> Here is a simplified implementation:
>
> # create table a (id int primary key, name varchar(128));
>
> # create table b (id int primary key, name varchar(128));
>
> # create table c (id int primary key, a_id int references a(id), b1_id 
> int references b(id), b2_id int references b(id), b3_id int references 
> b(id));
>
> # create view v as select c.id, c.a_id, c.b1_id, c.b2_id , c.b3_id, 
> a.name a_name, b1.name b1_name, b2.name b2_name, b3.name b3_name from 
> c,  a, b b1, b b2, b b3 where c.a_id=a.id and c.b1_id=b1.id and 
> c.b2_id=b2.id and c.b3_id=b3.id;
>
> When I try to get just info from tables c and b1:
>
> # select id, b1_name from v
>
> it still does all the joins (see below).
>
> I would expect just one join (due to the request of columns from the 
> two tables),
>
> since all joins are on foreign constrains referencing primary keys,
>
> there are no filters on the other tables, so it is guaranteed that the 
> useless joins will always return exactly one answer.

I think what you're asking for is a lot more complex than it sounds, and incorrect.  The precise state of the data
influenceshow many records come back (in this case, either 1 or 0), for example if b3_id is null you get zero rows.
Moreto the point, you *instructed* the server to make the join.  There are strategies to make joins 'optional' at run
timewith respect to a query, but they are more complicated than simply withdrawing columns from the select list.
 

Stepping back a bit, the query needs to be planned before peeking at the data in the tables.  The planner is able to
makeassumptions against a statistical picture of the data but shouldn't be expected to actually inspect precise result
datain order to generate a better plan.
 

merlin


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

Предыдущее
От: Kevin Grittner
Дата:
Сообщение: Re: PSQL does not remove obvious useless joins
Следующее
От: "Sfiligoi, Igor"
Дата:
Сообщение: Re: --EXTERNAL--Re: PSQL does not remove obvious useless joins