Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gCFZ9-0002nC-0U for pgsql-general@arkaria.postgresql.org; Tue, 16 Oct 2018 02:58:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gCFZ7-0002wa-13 for pgsql-general@arkaria.postgresql.org; Tue, 16 Oct 2018 02:58:37 +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 1gCFZ6-0002wS-Og for pgsql-general@lists.postgresql.org; Tue, 16 Oct 2018 02:58:36 +0000 Received: from ec2-34-255-138-49.eu-west-1.compute.amazonaws.com ([34.255.138.49] helo=zimbabwe.chezphil.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gCFZ0-0003Zy-IP for pgsql-general@postgresql.org; Tue, 16 Oct 2018 02:58:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=chezphil.org mappendix.com; s=20171026; h=From:Content-Type:MIME-Version: References:In-Reply-To:Message-ID:Subject:Date:Cc:To:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=zGpXd71YB2wV+BeYPqslEZMys9ILZDVEQ8d9RT+3pT4=; b=oyWo+vWA7BSIRUM64MPkBQwrp PZth0MZnBuY75np8uLvnRuXdFIB1a9e8u3Wgz8K6o62mBiBZ7Kbwwm9+XcxAfjoJoX6HGBvbQCOEX 5mCjKoqQngVAsFGh1aDvhewh0c80mWE9e5Ro+wRVcJR7X1lVRJSshJPDnXaYvLo7/d2XM=; Received: from localhost ([127.0.0.1] helo=chezphil.org) by zimbabwe.chezphil.org with esmtp (Exim 4.89) (envelope-from ) id 1gCFYz-00042p-CA for pgsql-general@postgresql.org; Tue, 16 Oct 2018 03:58:29 +0100 Received: from localhost ([::ffff:127.0.0.1]) by zimbabwe with smtp (dmsmtpd 0.00001); Tue, 16 Oct 2018 03:58:29 +0100 To: "Postgres General" Cc: "Tom Lane" Date: Tue, 16 Oct 2018 03:58:29 +0100 Subject: Re: Filtering before join with date_trunc() Message-ID: <1539658709144@dmwebmail.dmwebmail.chezphil.org> In-Reply-To: <2018.1539621952@sss.pgh.pa.us> References: <2018.1539621952@sss.pgh.pa.us> X-Mailer: Decimail Webmail 3alpha16 MIME-Version: 1.0 Content-Type: text/plain; format="fixed"; charset="UTF-8" From: "Phil Endecott" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thanks all for the replies. Tom Lane wrote: > You're expecting too much. That often seems to be the case. > 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 :-( Here's an example: create table t1 ("time" timestamptz, value1 numeric); create index t1_time on t1("time"); \copy t1 from ...... create table t2 ("time" timestamptz, value2 numeric); create index t2_time on t2("time"); \copy t2 from ...... explain select * from t1 join t2 using("time") where "time" between '2018-10-01' and '2018-10-02'; +------------------------------------------------------------------------------------------------+ | QUERY PLAN | +------------------------------------------------------------------------------------------------+ | Hash Join (cost=12.99..101.03 rows=138 width=21) | | Hash Cond: (t2."time" = t1."time") | | -> Seq Scan on t2 (cost=0.00..70.11 rows=4411 width=15) | | -> Hash (cost=11.18..11.18 rows=145 width=14) | | -> Index Scan using t1_time on t1 (cost=0.28..11.18 rows=145 width=14) | | Index Cond: (("time" >= '2018-10-01 00:00:00+00'::timestamp with time zone) AND .| |.("time" <= '2018-10-02 00:00:00+00'::timestamp with time zone)) | +------------------------------------------------------------------------------------------------+ explain with q1 as (select * from t1 where "time" between '2018-10-01' and '2018-10-02'), q2 as (select * from t2 where "time" between '2018-10-01' and '2018-10-02') select * from q1 join q2 using("time"); +------------------------------------------------------------------------------------------------+ | QUERY PLAN | +------------------------------------------------------------------------------------------------+ | Hash Join (cost=26.60..31.41 rows=136 width=72) | | Hash Cond: (q1."time" = q2."time") | | CTE q1 | | -> Index Scan using t1_time on t1 (cost=0.28..11.18 rows=145 width=14) | | Index Cond: (("time" >= '2018-10-01 00:00:00+00'::timestamp with time zone) AND ("ti.| |.me" <= '2018-10-02 00:00:00+00'::timestamp with time zone)) | | CTE q2 | | -> Index Scan using t2_time on t2 (cost=0.28..11.00 rows=136 width=15) | | Index Cond: (("time" >= '2018-10-01 00:00:00+00'::timestamp with time zone) AND ("ti.| |.me" <= '2018-10-02 00:00:00+00'::timestamp with time zone)) | | -> CTE Scan on q1 (cost=0.00..2.90 rows=145 width=40) | | -> Hash (cost=2.72..2.72 rows=136 width=40) | | -> CTE Scan on q2 (cost=0.00..2.72 rows=136 width=40) | +------------------------------------------------------------------------------------------------+ So.... as you say, even if I strip out all of the complexity of approximate timestamps and missing values, it's never going to push the BETWEEN filter down below the join. Even with just a few thousand rows I see a 5X speedup with the second query with the explicit filtering below the join. This is rather disappointing. Am I the only person who's ever wanted to do this? Regards, Phil.