Re: performance advice needed: join vs explicit subselect

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: performance advice needed: join vs explicit subselect
Date: 2009-01-27 18:48:11
Message-ID: 20090127184811.GJ3008@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jan 27, 2009 at 07:12:05PM +0100, Karsten Hilbert wrote:
> Hello all,
>
> maybe some general advice can be had on this:
>
> table test_results
> modified_by integer foreign key staff(pk),
> intended_reviewer integer foreign key staff(pk),
> actual_reviewer integer foreign key staff(pk)
>
> (this table will contain millions of rows)
>
> table staff
> pk integer
> name text
>
> (this table will contain at most 50 rows)
>
> Now I want to set up a view which aggregates test results
> with staff names for all three foreign keys. This would mean
> I would either have to
>
> - join test_results to staff three times, once for each
> of the foreign keys, this is going to be messy with
> tracking table aliases, duplicate column names etc

if you've only got three columns it shouldn't be too bad should it?

> - write three explicit sub-selects for the columns I want
> to denormalize into the view definition

This would look a bit prettier, but PG tends not to optimize at all. It
always executes it as a subplan and hence will only work nicely when
you've got a very small subset of the test_results coming back. PG will
*sometimes* remove subexpressions, but doesn't seem very predictable
about it:

SELECT id
FROM (
SELECT a.id, (SELECT b.name FROM bar b WHERE a.tid = b.tid)
FROM foo a) x;

PG seems to recognize that it can remove the subselect in the above
which is nice, but in other situations it doesn't seem to.

--
Sam http://samason.me.uk/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2009-01-27 19:04:02 Re: performance advice needed: join vs explicit subselect
Previous Message Alban Hertroys 2009-01-27 18:41:02 Re: How to do an UPDATE for all the fields that do NOT break a constraint?