Performance Postgres中的位运算

Performance Postgres中的位运算,performance,postgresql,indexing,bit-manipulation,Performance,Postgresql,Indexing,Bit Manipulation,我有以下表格: types | id | name ------+----+---------- 1 | A 2 | B 4 | C 8 | D 16| E 32| F 及 我现在想问: select id, name from vendors where type & 16 >0 //should return Jack as he is type E select id

我有以下表格:

types | id | name
------+----+----------
         1 | A
         2 | B
         4 | C
         8 | D
         16| E
         32| F

我现在想问:

select id, name from vendors where type & 16 >0 //should return Jack as he is type E
select id, name from vendors where type & 7 >0 //should return Ed, Iris, Jack
select id, name from vendors where type & 8 >0 //should return David, Ed, Felix, Herry 

postgres中表格
类型
供应商
的最佳索引是什么?我可能有数百万行的供应商。此外,与使用第三个表的多对多关系相比,使用这种按位方法有哪些折衷之处?哪个更好?

Use可以使用部分索引来解决“&”不是可索引运算符(afaik)的问题:

当然,每次添加新类型时都需要添加新索引。这是将数据扩展为关联表的原因之一,然后可以对关联表进行适当的索引。您始终可以编写触发器来另外维护位掩码表,但是使用多对多表来实际维护数据,因为这样会更清楚


如果您对扩展和性能的整个评估都是说“我可能有数百万行”,那么您还没有做足够的工作来开始进行这种优化。首先创建一个结构合理的清晰模型,然后根据其性能的实际统计数据对其进行优化。

我认为您的意思是“type&7=0”,如果使用“type&7>0”,您将返回与“a”、“B”或“C”匹配的任何项,因为匹配任何位都将导致答案大于0。(Alex、Bob、David、Ed、Goal、Henry、Iris、Jack)输入“type&7=0”只会得到与所有三位匹配的项目。(埃德、艾里斯、杰克)
select id, name from vendors where type & 16 >0 //should return Jack as he is type E
select id, name from vendors where type & 7 >0 //should return Ed, Iris, Jack
select id, name from vendors where type & 8 >0 //should return David, Ed, Felix, Herry 
CREATE INDEX vendors_typeA ON vendors(id) WHERE (type & 2) > 0;
CREATE INDEX vendors_typeB ON vendors(id) WHERE (type & 4) > 0;