2

我需要创建一个表(postgresql 9.1)并且我被卡住了。你能帮忙吗?

传入的数据可以采用以下两种格式之一:

  1. 客户 id(int), shop id(int), asof(date), 数量
  2. 客户 id(int), , asof(date), 数量

给定的传入 CSV 模板是:{client id, shop id, shop type, shop种类, asof, quantity}

第一种情况,key是--client id, shop id, asof

第二种情况,key是--client id, shop type, shop种类, asof

我试过类似的东西:

    create table(
            client_id       int references...,
            shop_id         int references...,
            shop_type       int references...,
            shop_genre      varchar(30),
            asof            date,
            quantity        real,
            primary key( client_id, shop_id, shop_type, shop_genre, asof )
    );

但后来我遇到了一个问题。当数据为格式 1 时,插入会因为 pk 中的空值而失败。

客户中的查询可以通过商店 ID 或商店类型和类型的组合进行。在流派上没有部分或正则表达式匹配的用例。

什么是合适的设计?我必须将其拆分为 2 个表,然后合并搜索结果吗?或者,是否习惯将 0 和空白作为缺失值并继续前进?

如果重要的话,一旦加载了所有历史数据,该表预计将有 100-5 亿行。

谢谢。

4

2 回答 2

3

您可以尝试部分唯一索引,也就是过滤的唯一索引,也就是条件唯一索引。 http://www.postgresql.org/docs/9.2/static/indexes-partial.html

基本上它归结为基于 where 子句过滤的唯一性,

例如(当然要测试正确性和对性能的影响):

CREATE TABLE client(
            pk_id           SERIAL,
            client_id       int,
            shop_id         int,
            shop_type       int,
            shop_genre      varchar(30),
            asof            date,
            quantity        real,
            PRIMARY KEY (pk_id)
    );


  CREATE UNIQUE INDEX uidx1_client
  ON client
  USING btree
  (client_id, shop_id, asof, quantity)
  WHERE client_id = 200;

  CREATE UNIQUE INDEX uidx2_client
  ON client
  USING btree
  (client_id, asof, quantity)
  WHERE client_id = 500;
于 2013-01-24T14:04:16.267 回答
1

一个简单的解决方案是为主键创建一个字段,该字段将使用两种算法之一根据传入的内容生成其数据。

如果您想要一个完全规范化的解决方案,您可能需要将商店信息拆分为两个单独的表,并使用外连接从该表中引用它。

您还可以使用 postgres 中可用的表继承。

于 2013-01-24T07:40:37.710 回答