pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Heikki Linnakangas <heikki@enterprisedb.com>
Cc: Marcin Stępnicki <mstepnicki@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: one column from huge view
Date: Thu, 12 Jul 2007 10:48:17 -0400
Message-ID: <17948.1184251697@sss.pgh.pa.us> (raw)
In-Reply-To: <4695EB62.7060307@enterprisedb.com>
References: <f74p19$1gb$1@sea.gmane.org>
	<4695EB62.7060307@enterprisedb.com>

Heikki Linnakangas <heikki@enterprisedb.com> writes:
> Marcin Stępnicki wrote:
>> Now, does PostgreSQL skip all the calculations from other columns and
>> executes this query faster then select * from huge_view?

> In simple cases, yes.

A rule of thumb is that it's been optimized if you don't see a "Subquery
Scan" node in the plan.  As an example:

regression=# create view v1 as select * from tenk1;
CREATE VIEW
regression=# create view v2 as select *,random() from tenk1;
CREATE VIEW
regression=# explain select unique1 from v1;
                        QUERY PLAN
-----------------------------------------------------------
 Seq Scan on tenk1  (cost=0.00..458.00 rows=10000 width=4)
(1 row)

regression=# explain select unique1 from v2;
                            QUERY PLAN
-------------------------------------------------------------------
 Subquery Scan v2  (cost=0.00..583.00 rows=10000 width=4)
   ->  Seq Scan on tenk1  (cost=0.00..483.00 rows=10000 width=244)
(2 rows)

If you want to look closer you can use EXPLAIN VERBOSE and count the
TARGETENTRY nodes in the targetlist for each plan node.  In the above
example, it's possible to see in the EXPLAIN VERBOSE output that the
Seq Scan node in the first plan is computing only the single variable
requested, whereas in the second plan the Seq Scan node is computing
all the outputs of the view (including the random() function call)
and then the Subquery Scan is projecting only a single column from
that result.

			regards, tom lane



view thread (5+ messages)

Message-ID: <17948.1184251697@sss.pgh.pa.us>
Permalink:  ../17948.1184251697@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/17948.1184251697@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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, heikki@enterprisedb.com, mstepnicki@gmail.com
  Subject: Re: one column from huge view
  In-Reply-To: <17948.1184251697@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