From: Adam Tauno Williams <awilliam@whitemice.org>
To: pgsql-performance@postgresql.org
Subject: Re: extract(year from date) doesn't use index but maybe could?
Date: Sun, 19 Apr 2015 17:03:47 -0400
Message-ID: <1429477427.11066.4.camel@whitemice.org> (raw)
In-Reply-To: <CADhq2b67QTAD7AkhGm3GZMwCUDU6kR70mpKEEnM=S5h1yRtdgg@mail.gmail.com>
References: <CADhq2b51G0QrjNz_vH6e_SLyu=tR0A0G0zHhFsP7rqRr+6FA3w@mail.gmail.com>
<5533E8F4.2050400@2ndquadrant.com>
<CADhq2b67QTAD7AkhGm3GZMwCUDU6kR70mpKEEnM=S5h1yRtdgg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
On Sun, 2015-04-19 at 13:10 -0700, Jon Dufresne wrote:
> On Sun, Apr 19, 2015 at 10:42 AM, Tomas Vondra
> <tomas.vondra@2ndquadrant.com> wrote:
> > On 04/19/15 19:16, Jon Dufresne wrote:
> >> Given the table:
> >> CREATE TABLE dates (id SERIAL, d DATE NOT NULL, t TEXT NOT NULL)
> >> With an *index* on field d. The following two queries are functionally
> >> equivalent:
> >> 1. SELECT * FROM dates WHERE d >= '1900-01-01'
> >> 2. SELECT * FROM dates WHERE EXTRACT(year from d) >= 1900'
> >> By functionally equivalent, they will return the same result set.
> >> Query 2 does not use the index, adding a performance cost. It seems
> >> there is an opportunity for optimization to handle these two queries
> >> equivalently to take advantage of the index.
> > Or you might try creating an expression index ..
> > CREATE INDEX date_year_idx ON dates((extract(year from d)));
> Certainly, but won't this add additional overhead in the form of two
> indexes; one for the column and one for the expression?
> My point is, why force the user to take these extra steps or add
> overhead when the the two queries (or two indexes) are functionally
> equivalent.
But they aren't functionally equivalent. One is an index on a
datetime/date, the other is an index just on the year [a DOUBLE].
Date/datetimes potentially have time zones, integer values do not - in
general time values are an order of magnitude more complicated than
people expect.
> Shouldn't this is an optimization handled by the database
> so the user doesn't need to hand optimize these differences?
Sometimes "d >= '1900-01-01'" and "EXTRACT(year from d) >= 1900" may be
equivalent; but not always.
--
Adam Tauno Williams <mailto:awilliam@whitemice.org> GPG D95ED383
Systems Administrator, Python Developer, LPI / NCLA
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: awilliam@whitemice.org
Subject: Re: extract(year from date) doesn't use index but maybe could?
In-Reply-To: <1429477427.11066.4.camel@whitemice.org>
* 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