pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert McGehee <rmcgehee@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Query optimization with repeated calculations
Date: Fri, 19 Jan 2018 12:15:13 -0500
Message-ID: <24253.1516382113@sss.pgh.pa.us> (raw)
In-Reply-To: <CAOpVXKoQY6qgHgM9kQbxiTheuTj=zvP-2-Q8+2w9008VsJu8AQ@mail.gmail.com>
References: <CAOpVXKoQY6qgHgM9kQbxiTheuTj=zvP-2-Q8+2w9008VsJu8AQ@mail.gmail.com>

Robert McGehee <rmcgehee@gmail.com> writes:
> I frequently want to make views that repeat a calculation over and over
> again in lots of columns. In the example below, let’s say it’s (a+b), but
> we can imagine the calculation being much more complicated.

> For example:
> CREATE VIEW AS
> SELECT (a+b)*c as c1, (a+b)*d as d1, (a+b)*e as e1
> FROM table;

> My question is, is PostgreSQL "smart" enough to cache this (a+b)
> calculation in the above example, or does it recalculate it in each column?

Postgres will not notice that there's duplicate subexpressions.

> If it recalculates it in each column, would I generally get better
> performance by using a subquery to convince PostgreSQL to cache the
> result?  For example:

> CREATE VIEW AS
> SELECT x*c as c1, x*d as d1, x*e as e1
> FROM (SELECT (a+b) as x, * FROM table) x;

Maybe, if the subexpression is expensive enough (a+b probably doesn't
meet that threshold).  You'd need to take care to prevent the
subquery from being "flattened" into the upper query.  Typically
this might require an optimization fence like OFFSET 0, which is
problematic for a view because it could prevent optimizations that
are far more important than avoiding duplicated calculations.
CTEs are likewise a rather blunt tool that could cost more than
they save.

It might work better to put the redundant calculations in a LATERAL
subquery, ie

CREATE VIEW AS
SELECT x*c as c1, x*d as d1, x*e as e1
FROM tab, LATERAL (SELECT a+b as x OFFSET 0) ss;

I haven't really experimented, but I think that this'd dodge
most of the optimization pitfalls, except one: in this formulation,
a+b will be evaluated for every "tab" row even if the particular
use of the view does not demand any of the columns that use "x".

			regards, tom lane




view thread (2+ messages)

Message-ID: <24253.1516382113@sss.pgh.pa.us>
Permalink:  ../24253.1516382113@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/24253.1516382113@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-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rmcgehee@gmail.com
  Subject: Re: Query optimization with repeated calculations
  In-Reply-To: <24253.1516382113@sss.pgh.pa.us>
  References: <CAOpVXKoQY6qgHgM9kQbxiTheuTj=zvP-2-Q8+2w9008VsJu8AQ@mail.gmail.com> <24253.1516382113@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