Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gH79z-0002IY-M8 for pgsql-general@arkaria.postgresql.org; Mon, 29 Oct 2018 13:00:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gH79x-0002Ns-Sw for pgsql-general@arkaria.postgresql.org; Mon, 29 Oct 2018 13:00: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 1gH79x-0002MG-KM for pgsql-general@lists.postgresql.org; Mon, 29 Oct 2018 13:00:45 +0000 Received: from mta2.recol.net ([64.207.103.7]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gH79v-00062q-13 for pgsql-general@postgresql.org; Mon, 29 Oct 2018 13:00:44 +0000 Received: from wrkhors.com (162-194-37-157.lightspeed.brhmal.sbcglobal.net [162.194.37.157]) by mta2.recol.net (Postfix) with ESMTPSA id C856439812E; Mon, 29 Oct 2018 09:00:39 -0400 (EDT) Date: Mon, 29 Oct 2018 07:58:41 -0500 From: Steven Lembark To: pgsql-general@postgresql.org Cc: lembark@wrkhors.com Subject: Re: Select "todays" timestamps in an index friendly way Message-ID: <20181029075841.3db03113.lembark@wrkhors.com> In-Reply-To: <20181023093801.GA21523@lutz-pc.ecm4u.intra> References: <20181023093801.GA21523@lutz-pc.ecm4u.intra> Organization: Workhorse Computing X-Mailer: Claws Mail 3.17.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > create temporary table t ( > id SERIAL primary key, > ts timestamp not null default now() > ); * add date( ts ) as a field and index date = now()::date. * Depending on the amount of data in your table the date may not be seletive enough to be worth using, at which point the index may be present and ignored. Only way to be sure is analyze it. * Might be worth looking at a partial index using >= 00:00:00 and < 24:00:00 (PG grocks the 2400 notation for "midnight at the end of today) or where ts && tsrange( ... 00:00:00, ... 24:00:00, '[)] ) Nice thing about the partial index is that you can create it on all of the non-ts fields for fast lookup by whatever and only index the portion for today. * Think about using a materialized view rather than a temp table. May prove simpler to query. -- Steven Lembark 3920 10th Ave South Workhorse Computing Birmingham, AL 35222 lembark@wrkhors.com +1 888 359 3508