我需要创建一个表(postgresql 9.1),但我陷入困境。你能帮忙吗?
传入数据可以采用以下两种格式之一:
- 客户 ID(int)、商店 ID(int)、asof(日期)、数量
- 客户 ID(int)、asof(日期)、数量
给定的传入 CSV 模板为:{客户 ID、商店 ID、商店类型、商店类型、asof、数量}
第一种情况,关键是 -- client id, shop id, asof
在第二种情况下,关键是 -- 客户 ID、商店类型、商店类型、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 亿行。
谢谢。
最佳答案
您可以尝试部分唯一索引,又名过滤唯一索引,又名条件唯一索引。 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;
关于postgreSQL表设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14494266/