我不认为有这样做的内置方法。但是,您可以使用表达式创建唯一索引。让我假设你在每个表都有一个串行唯一ID:
create unique index unq_events_timestamp_request_session_key
on (event_timestamp, request_id, session_id,
(case when event_id < 99999 then event_id else -1 end)
);
的表达实际上是说:“如果该键已经在表中,则忽略它的唯一约束”。
您可以通过消除这些强制所有但当前副本的独特约束:
create unique index unq_events_timestamp_request_session_key
on (event_timestamp, request_id, session_id,
(case when event_id in (1, 2, 3, 5, 8) then event_id
else -1
end)
);
这需要研究当前的重复。其实,你也可以做任何与筛选子句:
create unique index unq_events_timestamp_request_session_key
on (event_timestamp, request_id, session_id)
where event_id > 999999 ;
create unique index unq_events_timestamp_request_session_key
on (event_timestamp, request_id, session_id)
where event_id not in (1, 2, 3, 5, 8) ;
它的工作原理,并迫使这些列的组合是唯一的,所以感谢。展望未来,你知道对查询的影响是什么吗?即每周增加大约300万行,查询速度会比使用唯一约束时慢吗?谢谢。 –
@David Gard。插入会更慢,因为他们不得不维护一个额外的索引。您需要进行基准测试以确定是否会导致您遇到问题。有一点需要考虑的是,因为这是一个部分索引,所以它不会帮助任何你想运行的查询旧数据的查询,所以你可能会发现你自己需要2个索引,一个唯一的索引,非唯一的整个表。 – Gary
感谢您的信息,确实非常有帮助。 –