From: Vincent Veyron <vv.lists@wanadoo.fr>
To: Alexander Farber <alexander.farber@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Same condition in the CTE and in the subsequent JOIN using it
Date: Wed, 25 Apr 2018 21:32:00 +0200
Message-ID: <20180425213200.5173fd2d2f2052392948f98c@wanadoo.fr> (raw)
In-Reply-To: <CAADeyWj2uzPYFWOH3hhSJ94f=mdWV0MQ7vYb+fW_pXYmkKFoxw@mail.gmail.com>
References: <CAADeyWj2uzPYFWOH3hhSJ94f=mdWV0MQ7vYb+fW_pXYmkKFoxw@mail.gmail.com>
On Wed, 25 Apr 2018 17:45:39 +0200
Alexander Farber <alexander.farber@gmail.com> 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 = g.gid)
> JOIN words_social s ON (s.uid IN (g.player1, g.player2))
> WHERE s.social = in_social --
> CAN THIS BE REFERRED TO FROM BELOW?
> AND s.sid = 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 = in_social
> AND s.sid = in_sid
> AND m.action = 'play'
> GROUP BY c.day
> ORDER BY c.day;
>
> $func$ LANGUAGE sql STABLE;
>
> By looking at the above source code, do you think, that the condition being
> used twice (the s.social = in_social AND s.sid = 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 main query, you can do away with it entirely and just remove
> JOIN words_social s USING(uid)
> WHERE s.social = in_social
> AND s.sid = in_sid
--
Bien à vous, Vincent Veyron
https://compta.libremen.com
Logiciel libre de comptabilité générale en partie double
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: vv.lists@wanadoo.fr, alexander.farber@gmail.com
Subject: Re: Same condition in the CTE and in the subsequent JOIN using it
In-Reply-To: <20180425213200.5173fd2d2f2052392948f98c@wanadoo.fr>
References: <CAADeyWj2uzPYFWOH3hhSJ94f=mdWV0MQ7vYb+fW_pXYmkKFoxw@mail.gmail.com> <20180425213200.5173fd2d2f2052392948f98c@wanadoo.fr>
* 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