2012-09-25 48 views
1

例如,我存储interests乔为chemistryclassical-music(如两个单独的值),并且可以查询该表像where interests in ('chemistry', 'biochem'),它会使用索引来有效地找到人如乔。postgreSQL索引是否有多值字段?

PostgreSQL有这样一个数据类型,可以索引多个值以允许高级查询吗?查询的性能优势是什么?什么是重要的告诫

+2

为什么你不正常化你的设计?存储逗号分隔值几乎不是一个好主意。 –

+0

我明白它在传统数据库中不好,因为它不会索引多个值。但我只是觉得DB必须提供这个功能。但我更新了这个关于警告的问题,感谢你:-) – aitchnyu

+1

问题是误导/措辞不佳。您在问是否可以在包含逗号分隔值的文本字段中对值进行索引。 –

回答

2

如果使用数组类型,它可以被索引和索引都可以使用,检查这个话题:Can PostgreSQL index array columns?

更好的数据模型可能是一个更好的主意,但是这取决于你。

1

您可以使用hstore数据类型。你只会存储密钥,而不是值。 hstore列可以被索引,并且测试密钥的存在非常快。

喜欢的东西:

create table person 
(
    person_id integer not null primary key, 
    name text not null, 
    interests hstore 
); 

insert into person (person_id, name, interests) 
values 
(1, 'Joe', 'chemistry => NULL, biochem => null'::hstore); 

然后找到与特定利益的人,你可以使用:

select * 
from person 
where interests ? 'chemistry' 
    or interests ? 'biochem'; 

这是一个黑客攻击的一位,因为hstore数据类型用来存储键/值对,在这种情况下,您只需要键。