Re: Query on view radically slower than query on underlying table

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Craig James <craig_james(at)emolecules(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Query on view radically slower than query on underlying table
Date: 2011-03-02 15:39:58
Message-ID: AANLkTik2C2mRO5_pTffswqxvKrPRSjeN6dkxu4kfbr_T@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Mon, Feb 28, 2011 at 2:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Craig James <craig_james(at)emolecules(dot)com> writes:
>> Then I thought maybe putting a foreign-key constraint on table "my_version" would solve the problem:
>
>>    alter table my_version add constraint fk_my_view foreign key(version_id)
>>    references registry.version(version_id) on delete cascade;
>
>> That way, the planner would know that every key in table "my_version" has to also be in table "version", thus avoiding that part about "forcing the other join to be done in toto".  But the foreign-key constraint makes no difference, it still does the full join and takes 65 seconds.
>
> That's just wishful thinking I'm afraid.  The planner doesn't currently
> make any deductions whatsoever from the presence of a foreign key
> constraint; and even if it did, I'm not sure that this would help it
> decide that a join order constraint could safely be dropped.

I've previously mused on -hackers about teaching the planner the
concept of an inner-or-left-join; that is, a join that's guaranteed to
return the same results whichever way we choose to implement it.
Proving that an inner join is actually inner-or-left would allow the
join removal logic to consider removing it altogether, and would allow
reordering in cases that aren't otherwise known to be safe. Proving
that a left join is actually inner-or-left doesn't help with join
removal, but it might allow the join to be reordered. Maybe
"non-row-reducing-join" is better terminology than
"inner-or-left-join", but in any case I have a suspicion that inner
join removal will end up being implemented as a special case of
noticing that an inner join falls into this class.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Robert Haas 2011-03-02 15:44:33 Re: Vacuum problem due to temp tables
Previous Message Robert Haas 2011-03-02 15:29:21 Re: Anyone tried Flashcache with PostgreSQL?