Re: Calculation of unused columns

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Volker Grabsch <vog(at)notjusthosting(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Calculation of unused columns
Date: 2009-10-18 00:07:21
Message-ID: 26771.1255824441@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Volker Grabsch <vog(at)notjusthosting(dot)com> writes:
> I'm confused about the absence of a very simple optimization
> in PostgreSQL. Suppose we have a VIEW where some columns are
> expensive to be calculated:

> CREATE VIEW a AS
> SELECT
> (... expensive calculation ...) as expensive,
> count(*) as cheap
> FROM
> x;

> where "x" is a sufficiently large table. I would expect the
> following query to be very fast:

> SELECT cheap FROM a;

> However, it takes the same time as "SELECT * FROM a;".

I think your main error is in supposing that count(*) is cheap ...

PG will suppress unused columns when a view can be flattened,
but typically not otherwise. In particular a view involving
aggregates won't get flattened; but given that the aggregates
will force a whole-table scan anyway, I can't get that excited
about removing a subset of them.

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2009-10-18 01:41:34 Re: Calculation of unused columns
Previous Message Volker Grabsch 2009-10-17 22:58:16 Calculation of unused columns