pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Phil Endecott <spam_from_pgsql_lists@chezphil.org>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Filtering before join with date_trunc()
Date: Mon, 15 Oct 2018 12:45:52 -0400
Message-ID: <2018.1539621952@sss.pgh.pa.us> (raw)
In-Reply-To: <1539619061614@dmwebmail.dmwebmail.chezphil.org>
References: <1539619061614@dmwebmail.dmwebmail.chezphil.org>

"Phil Endecott" <spam_from_pgsql_lists@chezphil.org> writes:
> ...
> For each of these tables I have a view which rounds the timestamp 
> to the nearest minute, and ensures there is only one row per minute:

>  SELECT date_trunc('minute'::text, tbl."time") AS "time",
>     max(tbl.pressure) AS pressure
>    FROM tbl
>   GROUP BY (date_trunc('minute'::text, tbl."time"))
>   ORDER BY (date_trunc('minute'::text, tbl."time"));

> I then join these tables on the rounded time:

>  SELECT COALESCE(rain."time", pressures."time", temperatures."time") AS "time",
>     rain.rain,
>     pressures.pressure,
>     temperatures.temperature
>    FROM rain
>      FULL JOIN pressures USING ("time")
>      FULL JOIN temperatures USING ("time");

> The COALESCE for time and the full joins are needed because some 
> columns may be missing for some minutes.

> Now I'd like to find the values for a particular short time period:

> SELECT * FROM readings
> WHERE "time" BETWEEN '2018-10-01T00:00:00' AND '2018-10-01T24:00:00'

> This works, but it is inefficient; it seems to create all the rounded 
> data, do the join on all of it, and then filter on the time period.  
> Ideally it would filter the raw data, and then need to round and join 
> far fewer rows.

You're expecting too much.  In the first place, it's generally impossible
to push constraints down through full joins, because that could change the
results.  (For example, if we remove a row from one join input, that could
result in getting unwanted null-extended rows from the other join input.)
Maybe with a whole lot of analysis we could prove that applying the "same"
constraint to both join keys doesn't break anything, but there's no such
intelligence there now --- in general, we've expended little if any effort
on optimizing full joins.  It's also not very clear to me that we can do
anything at all with pushing down constraints that are expressed in terms
of a JOIN USING merged column; they don't really constrain either input
individually.

> But I've done some experiments and even if I remove all the rounding and 
> replace the full joins with regular joins, it still does sequential 
> scans on at least one of the tables:

I think you're also expecting the system to deduce that it can apply an
inequality on one join column to the other one.  It doesn't; only equality
constraints have any sort of transitivity logic.

So you'll need to write out the BETWEEN separately for each table,
and put it below the full join, which means you won't be able to
use those nice views :-(

			regards, tom lane




view thread (5+ messages)  latest in thread

Message-ID: <2018.1539621952@sss.pgh.pa.us>
Permalink:  ../2018.1539621952@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2018.1539621952@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, spam_from_pgsql_lists@chezphil.org
  Subject: Re: Filtering before join with date_trunc()
  In-Reply-To: <2018.1539621952@sss.pgh.pa.us>
  References: <1539619061614@dmwebmail.dmwebmail.chezphil.org> <2018.1539621952@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