alternative to bitmap index in postgresql

后端 未结 3 874
野趣味
野趣味 2021-01-19 19:06

I have a table with hundreds of millions rows with schema like below.

tabe AA {
 id integer primay key,
 prop0 boolean not null,
 prop1 boolean not null,
 p         


        
相关标签:
3条回答
  • 2021-01-19 19:44

    Your real problem is a bad schema design, not the index. The properties should be placed in a different table and your current table should link to that table using a many to many relation.

    The BIT datatype might also be of use, just check the manual.

    0 讨论(0)
  • 2021-01-19 19:54

    Create a multicolumn index on properties which are always or almost always queried. Or several multicolumn indexes if needed.

    The alternative, when you do not query the same properties almost always, is to make a tsvector column with words describing your data, maintained using trigger, for example

    prop0=true
    prop1=false
    prop2=4
    

    would be

    'propzero nopropone proptwo4'::tsvector
    

    index it using GIN and then use full text search for searching:

    where tsv @@ 'popzero & nopropone & proptwo4'::tsquery
    
    0 讨论(0)
  • 2021-01-19 19:55

    An index is only used if it actually speeds up the query which is not necessarily always the case. Especially with smallish tables (say thousands of rows) a full table scan ("seq scan" in the Postgres execution plan) might indeed be a lot faster.

    How many rows did the table have when you tried the statement? How did the query look like? Maybe there are other conditions that prevent the index usage. Did you analyze the table to have up-to-date statistics?

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