我不認爲有這樣做的內置方法。但是,您可以使用表達式創建唯一索引。讓我假設你在每個表都有一個串行唯一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
感謝您的信息,確實非常有幫助。 –