Re: JOIN issues (Left vs Right for sorting), and "Nested Loop" problem

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alban Hertroys <alban(at)magproductions(dot)nl>
Cc: Phoenix Kiula <phoenix(dot)kiula(at)gmail(dot)com>, "Postgres General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: JOIN issues (Left vs Right for sorting), and "Nested Loop" problem
Date: 2007-09-01 17:09:32
Message-ID: 21752.1188666572@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alban Hertroys <alban(at)magproductions(dot)nl> writes:
> I am kind of surprised that the planner doesn't understand that a
> foreign key with a unique constraint (which a primary key is) means
> there is a 0..1 to 1 relationship with the target table.

Hm? It correctly estimated that it'd get one row out for each index
probe:

-> Index Scan using tradecount_pkey on tradecount (cost=0.00..8.45 rows=1 width=16) (actual time=0.006..0.008 rows=1 loops=386)
Index Cond: (trades.id = tradecount.id)

I don't think there's anything wrong with this plan at all, at least for
queries that select only a few hundred rows in trades. It would switch
to a different plan if a large fraction of the tables had to be joined,
but for joining a small fraction it's hard to beat nested indexscans.

(Of course, I'm assuming that this is a small fraction --- we don't
actually know the total sizes of these tables...)

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-09-01 17:17:13 Re: WAL Archiving problem
Previous Message Alban Hertroys 2007-09-01 15:44:27 Re: Obtaining random rows from a result set