On Wed, 25 Apr 2018 17:45:39 +0200 Alexander Farber <alexander.farber@xxxxxxxxx> 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