开发者

SQL Server: Can I create a 'synthetic' relation that has no indexing or constraint checking overhead?

开发者 https://www.devze.com 2023-04-01 13:05 出处:网络
What I wish to do is ensure that the foreign key relation is maintained in the database schema but for performance reasons, not enforce the constrain or incur any indexing overheads. It\'s purpose is

What I wish to do is ensure that the foreign key relation is maintained in the database schema but for performance reasons, not enforce the constrain or incur any indexing overheads. It's purpose is purely to document开发者_如何学C the relationship. This is typically referred to as a "synthetic relation".


There is no indexing overhead with FKs in SQL Server as it creates no default indexes for them.

You can achieve the functionality you want with regards to constraint checking by disabling the FK.

CREATE TABLE T1
(
C1 INT PRIMARY KEY
)

CREATE TABLE T2
(
C1 INT 
)

/*Add FK without checking existing data*/
ALTER TABLE T2  WITH NOCHECK ADD  CONSTRAINT FK_T2_C1 FOREIGN KEY(C1)
REFERENCES T1 (C1)

/*Prevent checking of future data*/
ALTER TABLE T2 NOCHECK CONSTRAINT FK_T2_C1

As well as ensuring data integrity foreign key constraints can also be used by the optimiser to improve query plans (Ref: Point 9)

Have you identified the FK as causing a specific performance issue in your case?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号