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 1gC5Fa-0004YJ-PD for pgsql-general@arkaria.postgresql.org; Mon, 15 Oct 2018 15:57:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gC5FZ-0006F6-B6 for pgsql-general@arkaria.postgresql.org; Mon, 15 Oct 2018 15:57:45 +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 1gC5FZ-0006Ez-23 for pgsql-general@lists.postgresql.org; Mon, 15 Oct 2018 15:57:45 +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 1gC5FW-0005Sc-TH for pgsql-general@postgresql.org; Mon, 15 Oct 2018 15:57:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=chezphil.org mappendix.com; s=20171026; h=Content-Type:MIME-Version: Message-ID:Subject:Date:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=ajjyc0Wm7ZfVjMQ/cOveNpvpjfaZf4Jdz8BOTJZPc28=; b=OtH1gWUEu5gEvRlQzltIx6SNv kLSeE1n0GnuFR4K4Y50sm9IlY2WuzPsc1D2Fs5aBOTRgAQWmcIm2wYsUTtJEwbQHJXX8+LzF5mqO8 HMc0ozTEkGTZmLI8vgU4Xcx1r7tAbDRAEO2Bd2fbxMPxMEToQk8QFXW4uo7QCZ94DWjrI=; Received: from localhost ([127.0.0.1] helo=chezphil.org) by zimbabwe.chezphil.org with esmtp (Exim 4.89) (envelope-from ) id 1gC5FV-0006Oo-QE for pgsql-general@postgresql.org; Mon, 15 Oct 2018 16:57:41 +0100 Received: from localhost ([::ffff:127.0.0.1]) by zimbabwe with smtp (dmsmtpd 0.00001); Mon, 15 Oct 2018 16:57:41 +0100 From: "Phil Endecott" To: "Postgres General" Date: Mon, 15 Oct 2018 16:57:41 +0100 Subject: Filtering before join with date_trunc() Message-ID: <1539619061614@dmwebmail.dmwebmail.chezphil.org> X-Mailer: Decimail Webmail 3alpha16 MIME-Version: 1.0 Content-Type: text/plain; format="fixed"; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Dear Experts, I have a few tables with "raw" timestamsps like this: +-------------------------------+----------+ | time | pressure | +-------------------------------+----------+ | 2018-09-14 00:00:07.148378+00 | 1007.52 | | 2018-09-14 00:10:07.147506+00 | 1007.43 | | 2018-09-14 00:20:07.147533+00 | 1007.28 | +-------------------------------+----------+ 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"); +------------------------+------+----------+-------------+ | time | rain | pressure | temperature | +------------------------+------+----------+-------------+ | 2018-09-14 00:00:00+00 | 0 | 1007.52 | 11.349 | | 2018-09-14 00:10:00+00 | 0 | 1007.43 | 11.2317 | | 2018-09-14 00:20:00+00 | 0 | 1007.28 | 11.2317 | +------------------------+------+----------+-------------+ 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. It would not be difficult for me to round the timestamps when inserting the data, and also ensure that there is only one row for each minute. 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: Nested Loop (cost=12.95..144.99 rows=135 width=20) Join Filter: (x_rain."time" = x_pressures."time") -> Hash Join (cost=12.67..97.83 rows=135 width=24) Hash Cond: (x_temperatures."time" = x_rain."time") -> Seq Scan on x_temperatures (cost=0.00..67.50 rows=4350 width=12) -> Hash (cost=10.98..10.98 rows=135 width=12) -> Index Scan using x_rain_by_time on x_rain (cost=0.28..10.98 rows=135 width=12) 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)) -> Index Scan using x_pressures_by_time on x_pressures (cost=0.28..0.34 rows=1 width=12) Index Cond: ("time" = x_temperatures."time") Maybe that is because the tables are currently relatively small (a few thousands rows) and it believes that sequential scans are faster. (I have sometimes wished for an "explain" variant that tells me what query plan it would choose if all the tables were 100X larger.) Is there anything I can do to make this more efficient when the tables are larger? Thanks for any suggestions. Regards, Phil.