Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBQ9A-0004eV-3D for pgsql-general@arkaria.postgresql.org; Wed, 25 Apr 2018 19:32:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBQ98-0006Sp-Ny for pgsql-general@arkaria.postgresql.org; Wed, 25 Apr 2018 19:32:06 +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.89) (envelope-from ) id 1fBQ98-0006Se-Fr for pgsql-general@lists.postgresql.org; Wed, 25 Apr 2018 19:32:06 +0000 Received: from smtp09.smtpout.orange.fr ([80.12.242.131] helo=smtp.smtpout.orange.fr) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fBQ94-0005MG-Pn for pgsql-general@postgresql.org; Wed, 25 Apr 2018 19:32:05 +0000 Received: from X201.home ([90.53.38.96]) by mwinf5d17 with ME id ejY01x00B24U7zN03jY0BS; Wed, 25 Apr 2018 21:32:01 +0200 X-ME-Helo: X201.home X-ME-Date: Wed, 25 Apr 2018 21:32:01 +0200 X-ME-IP: 90.53.38.96 Date: Wed, 25 Apr 2018 21:32:00 +0200 From: Vincent Veyron To: Alexander Farber Cc: pgsql-general Subject: Re: Same condition in the CTE and in the subsequent JOIN using it Message-Id: <20180425213200.5173fd2d2f2052392948f98c@wanadoo.fr> In-Reply-To: References: X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 25 Apr 2018 17:45:39 +0200 Alexander Farber wrote: > WITH cte AS ( > SELECT > DATE_TRUNC('day', m.played) AS day, > m.mid, > EXTRACT(EPOCH FROM m.played - LAG(m.played) OVER > (PARTITION BY m.gid ORDER BY m.played))::int/60 AS diff > FROM words_moves m > JOIN words_games g ON (m.gid =3D g.gid) > JOIN words_social s ON (s.uid IN (g.player1, g.player2= )) > WHERE s.social =3D in_social = -- > CAN THIS BE REFERRED TO FROM BELOW? > AND s.sid =3D in_sid > AND m.played > CURRENT_TIMESTAMP - interval '1 month' > ) > SELECT > TO_CHAR(c.day, 'DD.MM.YYYY'), > ROUND(AVG(c.diff)), > ROUND(AVG(m.score), 1) > FROM words_moves m > JOIN cte c using(mid) > JOIN words_social s USING(uid) > WHERE s.social =3D in_social > AND s.sid =3D in_sid > AND m.action =3D 'play' > GROUP BY c.day > ORDER BY c.day; >=20 > $func$ LANGUAGE sql STABLE; >=20 > By looking at the above source code, do you think, that the condition bei= ng > used twice (the s.social =3D in_social AND s.sid =3D in_sid) is "too much= " and > can be optimized? :-) I would say so, because as you've already applied the filter in the CTE it = won't have any effect. But anyway, since you are not using any column from words_social in your ma= in query, you can do away with it entirely and just remove=20 > JOIN words_social s USING(uid) > WHERE s.social =3D in_social > AND s.sid =3D in_sid --=20 Bien =E0 vous, Vincent Veyron=20 https://compta.libremen.com Logiciel libre de comptabilit=E9 g=E9n=E9rale en partie double