postgreSQL是否索引多值字段



例如,我将 Joe 的interests存储为 chemistryclassical-music(作为两个单独的值),您可以像where interests in ('chemistry', 'biochem')一样查询表,它将使用索引有效地查找像 Joe 这样的人。

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

如果您使用数组数据类型,则可以对其进行索引并且可以使用索引,请查看本主题:PostgreSQL 可以索引数组列吗?

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

你可以为此使用 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数据类型旨在存储键/值对,在这种情况下,您只需要键。

最新更新