Re: where clauses and multiple tables

From: David W Noon <dwnoon(at)ntlworld(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: where clauses and multiple tables
Date: 2009-09-09 11:42:31
Message-ID: 20090909124231.0e7e9935@dwnoon.ntlworld.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 08 Sep 2009 18:50:49 -0700, John R Pierce wrote about Re:
[GENERAL] where clauses and multiple tables:

>Yaroslav Tykhiy wrote:
>> By the way, folks, do you think there may be performance gain or
>> loss from rewriting this with an explicit JOIN? E.g.:
>>
>> SELECT DISTINCT foo.foo_id, foo.name FROM foo JOIN bar ON foo.bar_id
>> = bar.bar_id WHERE bar.name='martini';
>
>I would expect that to be more efficient as its the 'proper' SQL way
>of doing things,

Actually, since the "bar" table does not supply any of the result
columns, the IN predicate is a more idiomatic (or "proper") way of
coding the query.

>and the optimizer will do a better job on it,
>especially if foo.bar_id is a FK to bar.bar_id's primary key.

The optimizer *should* produce the same plan, either way.

>btw, can't this be written...
>
> SELECT DISTINCT foo.foo_id, foo.name
> FROM foo JOIN bar ON bar_id
> WHERE bar.name='martini';

The DISTINCT qualifier potentially changes the semantics, so the
immediate answer is "No".
--
Regards,

Dave [RLU #314465]
=======================================================================
david(dot)w(dot)noon(at)ntlworld(dot)com (David W Noon)
=======================================================================

In response to

Browse pgsql-general by date

  From Date Subject
Next Message chen jia 2009-09-09 13:05:58 Re: How to store data on an external drive
Previous Message Berge Schwebs Bjørlo 2009-09-09 10:17:33 WAL archiving file name collision