PostgreSQL daterange not using index correctly

后端 未结 1 683
陌清茗
陌清茗 2021-01-20 09:21

I have a simple table which has a user_birthday field with a type of date (which can be NULL value)

CREATE TABLE users
(
  user_id bigserial NOT NULL,
  user         


        
相关标签:
1条回答
  • 2021-01-20 09:24

    BETWEEN includes upper and lower border. Your condition

    WHERE user_birthday BETWEEN '1978-07-15'::date AND '1983-03-01'::date
    

    matches

    WHERE user_birthday <@ daterange('[1978-07-15,1983-03-01]')

    I see you mention a btree index. For that use simple comparison operators.

    Detailed manual page on which index is good for which operators.

    The range type operators <@ or @> would work with GiST indexes.
    Example:
    Perform this hours of operation query in PostgreSQL

    0 讨论(0)
提交回复
热议问题