相关疑难解决方法(0)

什么时候应该使用唯一约束而不是唯一索引?

当我希望一列具有不同的值时,我可以使用约束

create table t1(
id int primary key,
code varchar(10) unique NULL
);
go
Run Code Online (Sandbox Code Playgroud)

或者我可以使用唯一索引

create table t2(
id int primary key,
code varchar(10) NULL
);
go

create unique index I_t2 on t2(code);
Run Code Online (Sandbox Code Playgroud)

具有唯一约束的列似乎是唯一索引的良好候选者。

是否有任何已知的原因使用唯一约束而不是使用唯一索引?

index sql-server constraint best-practices unique-constraint

238
推荐指数
4
解决办法
11万
查看次数

如果表在多列上具有唯一约束,如何不“复制”表?

我有一个非常大的表 (35GB),它在四个列的组合中是独一无二的。

该表不是很宽,它唯一的四列是较大的列(以字节为单位)。最终结果是保持表唯一的索引是 21GB。这不是索引大小随时间膨胀的结果,而是索引创建后立即的大小。

我根本不需要优化插入速度,因为插入每月只会分批进行一次。一旦插入,任何行都不会进行任何更新。

我正在运行 PostgreSQL 9.5.0。

有没有办法不复制如此大的数据库部分来强制执行唯一约束?可能使用聚集索引之类的东西?

全表说明:

CREATE TABLE medi_cal_base_eligibility (
    client_index_number text NOT NULL,
    medi_cal_date date NOT NULL,
    eligibility_date date NOT NULL,
    aidcode text,
    responsible_county text,
    status text,
    cardinal smallint NOT NULL,
    id SERIAL PRIMARY KEY
);
Run Code Online (Sandbox Code Playgroud)

索引:

"medi_cal_base_eligibility_pkey" PRIMARY KEY, btree 
    (id)
"medi_cal_base_eligibility_uq_dates_cin_cardinal" UNIQUE CONSTRAINT, btree 
    (eligibility_date, client_index_number, medi_cal_date, cardinal)
Run Code Online (Sandbox Code Playgroud)

postgresql index-tuning postgresql-9.5

5
推荐指数
1
解决办法
219
查看次数