加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL hstore数组列的索引

发布时间:2020-12-13 16:34:38 所属栏目:百科 来源:网络整理
导读:我知道你可以在hstore列的字段上创建索引. 我知道你也可以在数组列上创建一个GIN索引. 但是在hstore数组上创建索引的语法是什么? 例如 CREATE TABLE customer ( pk serial PRIMARY KEY,customer hstore,customer_purchases hstore[]); 假设客户购买hstore可
我知道你可以在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数组中的索引字段.

我觉得你误解了PostgreSQL Arrays. 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?

如果您必须在此处根据“购买”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类型的练习吗?或者你是否有一些真正的用例会使你对真实数据的所有混淆都值得?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读