【发布时间】:2012-02-11 03:42:13
【问题描述】:
我有以下表格:
types | id | name
------+----+----------
1 | A
2 | B
4 | C
8 | D
16| E
32| F
和
vendors | id | name | type
--------+----+----------+-----
1 | Alex | 2 //type B only
2 | Bob | 5 //A,C
3 | Cheryl | 32 //F
4 | David | 43 //F,D,A,B
5 | Ed | 15 //A,B,C,D
6 | Felix | 8 //D
7 | Gopal | 4 //C
8 | Herry | 9 //A,D
9 | Iris | 7 //A,B,C
10| Jack | 23 //A,B,C,E
我现在想查询:
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 中表types 和vendors 的最佳索引是什么?我可能在供应商中有数百万行。此外,与使用第三个表的多对多关系相比,使用这种按位方法的权衡是什么?哪个更好?
【问题讨论】:
-
我认为你的意思是'type & 7 = 0',如果你使用'type & 7 >0',你将返回任何匹配'A'、'B'或'C'的项目,因为匹配任何位都会得到大于 0 的答案。(Alex、Bob、David、Ed、Goal、Henry、Iris、Jack)使“type & 7 = 0”只导致匹配所有三个位的那些项目。 (艾德、艾瑞斯、杰克)
标签: performance postgresql indexing bit-manipulation