Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ecaG7-0000Zi-Nr for pgsql-general@arkaria.postgresql.org; Fri, 19 Jan 2018 17:15:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ecaG6-0001Wz-7t for pgsql-general@arkaria.postgresql.org; Fri, 19 Jan 2018 17:15:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ecaG5-0001Wh-Vo for pgsql-general@lists.postgresql.org; Fri, 19 Jan 2018 17:15:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ecaG3-0007oO-Bs for pgsql-general@postgresql.org; Fri, 19 Jan 2018 17:15:17 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w0JHFDpr024254; Fri, 19 Jan 2018 12:15:13 -0500 From: Tom Lane To: Robert McGehee cc: pgsql-general@postgresql.org Subject: Re: Query optimization with repeated calculations In-reply-to: References: Comments: In-reply-to Robert McGehee message dated "Fri, 19 Jan 2018 11:03:48 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <24252.1516382113.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 19 Jan 2018 12:15:13 -0500 Message-ID: <24253.1516382113@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Robert McGehee writes: > I frequently want to make views that repeat a calculation over and over > again in lots of columns. In the example below, let=E2=80=99s say it=E2=80= =99s (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 colu= mn? 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