pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Zot O'Connor <zot@zotconsulting.com>
Cc: postgres sql <pgsql-sql@hub.org>
Subject: Re: [SQL] Nested Views take forever
Date: Thu, 11 Nov 1999 19:34:06 -0500
Message-ID: <21836.942366846@sss.pgh.pa.us> (raw)
In-Reply-To: <382B4CD1.E7EB3C79@zotconsulting.com>

"Zot O'Connor" <zot@zotconsulting.com> writes:
> consup=> EXPLAIN SELECT COUNT(*) from depth3;
> NOTICE:  QUERY PLAN:
> Aggregate  (cost=11.69 rows=233 width=4)
>   ->  Seq Scan on subcat  (cost=11.69 rows=233 width=4)
>         SubPlan
>           ->  Seq Scan on subcat  (cost=11.69 rows=233 width=4)
>                 SubPlan
>                   ->  Seq Scan on subcat  (cost=11.69 rows=6 width=4)

The problem here is not views per se, it's that WHERE x in (sub-select)
is not a very efficient construct --- it basically always generates a
nested-loop plan.  What you've got above is O(N^3) for an N-tuple table.

Try something like this instead:

CREATE VIEW depth2 AS SELECT ... FROM subcat, depth1 WHERE
	subcat.scatscat = depth1.scatval;

CREATE VIEW depth3 AS SELECT ... FROM subcat, depth2 WHERE
	subcat.scatscat = depth2.scatval;

Given indexes on scatscat and scatval, I'd expect this to produce a
merge-join plan, which should be reasonably quick --- better than
O(N^2) or O(N^3) anyway.

There's been some talk of reimplementing WHERE ... IN ... so that it
does something intelligent without help, but there are a lot of
higher-priority problems on the TODO list...

			regards, tom lane



view thread (3+ messages)  latest in thread

Message-ID: <21836.942366846@sss.pgh.pa.us>
Permalink:  ../21836.942366846@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/21836.942366846@sss.pgh.pa.us

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, zot@zotconsulting.com, pgsql-sql@hub.org
  Subject: Re: [SQL] Nested Views take forever
  In-Reply-To: <21836.942366846@sss.pgh.pa.us>
  References: <382B4CD1.E7EB3C79@zotconsulting.com> <21836.942366846@sss.pgh.pa.us>

* 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