Re: extract(year from date) doesn't use index but maybe could?

Поиск
Список
Период
Сортировка
От Tomas Vondra
Тема Re: extract(year from date) doesn't use index but maybe could?
Дата
Msg-id 5533E8F4.2050400@2ndquadrant.com
обсуждение исходный текст
Ответ на extract(year from date) doesn't use index but maybe could?  (Jon Dufresne <jon.dufresne@gmail.com>)
Ответы Re: extract(year from date) doesn't use index but maybe could?  (Jon Dufresne <jon.dufresne@gmail.com>)
Список pgsql-performance

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)));


regards

--
Tomas Vondra                  http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


В списке pgsql-performance по дате отправления:

Предыдущее
От: Jon Dufresne
Дата:
Сообщение: extract(year from date) doesn't use index but maybe could?
Следующее
От: Jon Dufresne
Дата:
Сообщение: Re: extract(year from date) doesn't use index but maybe could?