Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id C0F379FBEAE for ; Thu, 12 Jul 2007 11:48:27 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 56804-04-2 for ; Thu, 12 Jul 2007 11:48:15 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 46E1B9FBE7C for ; Thu, 12 Jul 2007 11:48:24 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l6CEmH5C017949; Thu, 12 Jul 2007 10:48:17 -0400 (EDT) To: Heikki Linnakangas cc: =?UTF-8?B?TWFyY2luIFN0xJlwbmlja2k=?= , pgsql-performance@postgresql.org Subject: Re: one column from huge view In-reply-to: <4695EB62.7060307@enterprisedb.com> References: <4695EB62.7060307@enterprisedb.com> Comments: In-reply-to Heikki Linnakangas message dated "Thu, 12 Jul 2007 09:50:42 +0100" Date: Thu, 12 Jul 2007 10:48:17 -0400 Message-ID: <17948.1184251697@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/143 X-Sequence-Number: 25827 Heikki Linnakangas 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