X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AA5FD3A3BFE for ; Fri, 5 Nov 2004 09:13:20 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 17565-01 for ; Fri, 5 Nov 2004 09:13:11 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id BA6CD3A3B75 for ; Fri, 5 Nov 2004 09:13:12 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id iA59D6gN014786 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 5 Nov 2004 02:13:08 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id iA59D6ot045308; Fri, 5 Nov 2004 02:13:06 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id iA59D6oK045307; Fri, 5 Nov 2004 02:13:06 -0700 (MST) (envelope-from mfuhr) Date: Fri, 5 Nov 2004 02:13:06 -0700 From: Michael Fuhr To: Michael Glaesemann Cc: Antony Paul , pgsql-performance@postgresql.org Subject: Re: Checking = with timestamp field is slow Message-ID: <20041105091306.GA45244@winnie.fuhr.org> References: <2989532e0411042316566b35f4@mail.gmail.com> <4704C044-2F05-11D9-BE4A-000A95C88220@myrealbox.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4704C044-2F05-11D9-BE4A-000A95C88220@myrealbox.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/92 X-Sequence-Number: 9046 On Fri, Nov 05, 2004 at 05:32:49PM +0900, Michael Glaesemann wrote: > > On Nov 5, 2004, at 5:14 PM, Michael Glaesemann wrote: > > > > >On Nov 5, 2004, at 4:16 PM, Antony Paul wrote: > >>where today::date = '2004-11-05'; > >> > >>This is the only condition in the query. There is a btree index on the > >>column today. > >>Is there any way to optimise it. > > > >I'm sure others out there have better ideas, but you might want to try > > > >where current_date = date '2004-11-05' > > Ach! just re-read that. today is one of your columns! Try > > where today::date = date '2004-11-05' Casting '2004-11-05' to DATE shouldn't be necessary, at least not in 7.4.5. test=> EXPLAIN ANALYZE SELECT * FROM foo WHERE today::DATE = '2004-11-05'; QUERY PLAN -------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..268.00 rows=50 width=16) (actual time=0.592..50.854 rows=1 loops=1) Filter: ((today)::date = '2004-11-05'::date) As you can see, '2004-11-05' is already cast to DATE. The sequential scan is happening because there's no index on today::DATE. test=> CREATE INDEX foo_date_idx ON foo (DATE(today)); CREATE INDEX test=> EXPLAIN ANALYZE SELECT * FROM foo WHERE DATE(today) = '2004-11-05'; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------- Index Scan using foo_date_idx on foo (cost=0.00..167.83 rows=50 width=16) (actual time=0.051..0.061 rows=1 loops=1) Index Cond: (date(today) = '2004-11-05'::date) -- Michael Fuhr http://www.fuhr.org/~mfuhr/