pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dean Gibson (DB Administrator) <postgresql@ultimeth.com>
To: pgsql-performance@postgresql.org
Subject: Re: Q on views and performance
Date: Sat, 23 Feb 2008 07:08:57 -0800
Message-ID: <47C03709.5010205@ultimeth.com> (raw)
In-Reply-To: <c2350ba40802230559l280eab73k8e79a8bda7a4994a@mail.gmail.com>
References: <c2350ba40802221249h37913d0cyaa6387a93c55fe69@mail.gmail.com>
	<47BF7B80.1040309@ultimeth.com>
	<c2350ba40802230559l280eab73k8e79a8bda7a4994a@mail.gmail.com>

On 2008-02-23 05:59, Kynn Jones wrote:
> On Fri, Feb 22, 2008 at 8:48 PM, Dean Gibson (DB Administrator) 
> <postgresql@ultimeth.com <mailto:postgresql@ultimeth.com>> wrote:
>
>     ...
>
>
> Since you have experience working with views, let me ask you this. 
>  The converse strategy to the one I described originally would be to 
> create the individual tables T1, T2, T3, ..., T100, but instead of 
> keeping around the original (and now redundant) table T, replace it 
> with a view V made up of the union of T1, T2, T3, ..., T100.  The 
> problem with this alternative is that one cannot index V, or define a 
> primary key constraint for it, because it's a view.  This means that a 
> search in V, even for a primary key value, would be *have to be* very 
> inefficient (i.e. I don't see how even the very clever PostgreSQL 
> implementers could get around this one!), because the engine would 
> have to search *all* the underlying tables, T1 through T100, even if 
> it found the desired record in T1, since it has no way of knowing that 
> the value is unique all across V.
>
> Is there a way around this?
>
> kynn
>
Oh, I wouldn't create separate tables and do a UNION of them, I'd think 
that would be inefficient.

I didn't look in detail at your previous eMail, but I will now:

1. You haven't told us the distribution of "zipk", or what the tables 
are indexed on, or what type of performance you are expecting.  Your 
initial examples don't help much unless you actually have performance 
numbers or EXPLAIN output for them, since adding the third JOIN 
significantly changes the picture, as does changing one of the JOINs to 
a LEFT JOIN.

2. In your actual (Q1** and Q2**) examples, why is one JOIN an INNER 
JOIN and the other one a LEFT JOIN?  Given your description of Q1 at the 
top of your message, that doesn't make sense to me.

3. Why not write:

CREATE VIEW txt AS
  SELECT a1.word AS word1, a1.type AS type1, a2.word AS word2, a2.type 
AS type2
    FROM T a1 [LEFT] JOIN T a2 USING( zipk );  -- Use "LEFT" if appropriate
SELECT word1, word1
  FROM S JOIN txt ON word = word1
  WHERE type1 = <int1> AND type2 = <int2>;

If either of those (either with or without the "LEFT") are not 
equivalent to your problem, how about just:

SELECT a1.word AS word1, a2.word AS word2
  FROM S JOIN T a1 USING( word)
    [LEFT] JOIN T a2 USING( zipk )  -- Use "LEFT" if appropriate
  WHERE a1.type = <int1> AND a2.type = <int2>;

Show us (using EXPLAIN) what the query planner thinks of each of these.

-- 
Mail to my list address MUST be sent via the mailing list.
All other mail to my list address will bounce.

view thread (15+ messages)  latest in thread

Message-ID: <47C03709.5010205@ultimeth.com>
Permalink:  ../47C03709.5010205@ultimeth.com/
Also on:    postgresql.org/message-id/47C03709.5010205@ultimeth.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: postgresql@ultimeth.com
  Subject: Re: Q on views and performance
  In-Reply-To: <47C03709.5010205@ultimeth.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox