PostgreSQL: Create index for boolean column

前端 未结 1 397
误落风尘
误落风尘 2020-12-15 05:28

I have a table that has one boolean column.

productid integer
isactive boolean

When I execute the query

SELECT productid           


        
相关标签:
1条回答
  • 2020-12-15 06:12

    PostgreSQL will use an index only if it thinks it will be cheaper that way. An index on a boolean column, which can only take two possible values, will almost never be used, because it is cheaper to sequentially read the whole table than to use random I/O on the index and the table if a high percantage of the table has to be retrieved.

    An index on a boolean column is only useful

    1. in data warehouse scenarios, where it can be combined with other indexes via a bitmap index scan.

    2. if only a small fraction of the table has the value TRUE (or FALSE for that matter). In this case it is best to create a partial index like

      CREATE INDEX ON mytab((1)) WHERE boolcolumn;
      
    0 讨论(0)
提交回复
热议问题