我知道您可以在hstore列中的字段上创建索引。我知道您还可以在数组列上创建GIN索引。
但是,在hstore数组上创建索引的语法是什么?
例如:
CREATE TABLE customer (
pk serial PRIMARY KEY,
customer hstore,
customer_purchases hstore[]
);假设客户购买hstore可能是这样的散列
productId -> 1
price -> 9.99我在customer_purchases hstore[]中有一个数组
我想在customer.customer_purchases[]-> productId上创建索引
这个是可能的吗?我尝试过不同的CREATE INDEX语法组合,但它们似乎都不支持hstore数组中的索引字段。
发布于 2012-04-12 12:14:21
我想你误解了PostgreSQL Array。Array实际上只是一个字符串。您不能索引数组中的对象(在本例中为HSTOREs),原因很简单,因为它不是TABLE。
相反,创建一个额外的表:
CREATE TABLE customer (
pk bigserial PRIMARY KEY,
customer hstore
);
CREATE TABLE purchases (
pk bigserial PRIMARY KEY,
customer_pk bigint not null,
purchase hstore not null,
constraint "must be a valid customer!"
foreign key (customer_pk) references customer(pk)
);另外,您为什么在这里使用HSTORE%s?
如果您必须在此处创建基于"purchase" HSTORE的INDEX,请执行以下操作:
CREATE OR REPLACE FUNCTION purchase_amount(purchase hstore) returns float as $$
select ($1 -> 'price')::float;
$$ language 'SQL' IMMUTABLE;
CREATE INDEX "purchases by price" ON purchases (purchase_amount(purchase));这只是一个理解HSTORE类型的练习吗?或者,您是否有一些真实的用例,可以让您的真实数据的所有这些混淆是值得的?
https://stackoverflow.com/questions/10115152
复制相似问题