Untrusted foreign keys and constraints can degrade your SQL Server performance

Disable keys and constraints to improve the performance of load data is a common practice in large data warehouses and other environments with big tables.

This technique is relatively more natural and safer than dropping and recreating all the keys and constraints if you don’t forget to enable them after the work has been done.

Having untrusted or disabled FOREIGN KEY or CHECK constraints in your database may cause query performance problems with inadequate query plans and degrade data consistency and integrity.

How to check for Untrusted Foreign Keys and Constraints?

Use the script below to find untrsted untrusted foreign Keys:

SELECT '[' + s.name + '].[' + o.name + '].[' + i.name + ']' AS KeyName
FROM sys.foreign_keys i
INNER JOIN sys.objects o ON i.parent_object_id = o.object_id
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE i.is_not_trusted = 1 AND i.is_not_for_replication = 0;

Use the  following  script to get the list of  untrusted constraints:

SELECT '[' + s.name + '].[' + o.name + '].[' + i.name + ']' AS KeyName
FROM sys.check_constraints i
INNER JOIN sys.objects o ON i.parent_object_id = o.object_id
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE i.is_not_trusted = 1 AND i.is_not_for_replication = 0 AND i.is_disabled = 0;

How to restore the trust for Foreign Keys and Constraints?

Once you have that result set above, you can use the alter table statement to re-establish the trust for each result in the list. Just replace “MyTableName” and “MyConstraintName” to your table name and Foreign Keys/Constraints name in the code below.

ALTER TABLE MyTableName WITH CHECK CHECK CONSTRAINT MyConstraintName

Alternatively, you can also run the script below that will generate the necessary T-SQL to recheck all the Untrusted constraints:

SELECT N'ALTER TABLE ' 
    + QUOTENAME(s.name) + N'.' + QUOTENAME(o.name) 
    + N' WITH CHECK CHECK CONSTRAINT ' 
    + QUOTENAME(c.name) + N';' AS ConstraintsToFix
FROM sys.check_constraints c
INNER JOIN 
sys.objects o ON c.parent_object_id = o.object_id
INNER JOIN 
sys.schemas s ON o.schema_id = s.schema_id
WHERE c.is_not_trusted = 1 
AND c.is_not_for_replication = 0 
AND c.is_disabled = 0

The following script will generate the necessary T-SQL to recheck the untrusted foreign keys:

SELECT N'ALTER TABLE ' + QUOTENAME(s.name) 
  + N'.' + QUOTENAME(o.name) + N' WITH CHECK CHECK CONSTRAINT ' 
  + QUOTENAME(f.name) + N';' AS ForeignKeystoFix
FROM    sys.foreign_keys f
INNER JOIN 
 sys.objects o ON f.parent_object_id = o.object_id
INNER JOIN 
 sys.schemas s ON o.schema_id = s.schema_id
WHERE f.is_not_trusted = 1 
 AND f.is_not_for_replication = 0

More information:

Microsoft – Unique Constraints and Check Constraints
Managing untrusted foreign keys, Jefferson Elias – SQLShack
Avoid Untrusted Constraints in SQL Server, Armando Prato – MSSQLTips

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

4.8/5

42

TESTIMONIALS

FREE Scripts to Instantly
Improve SQL Server Performance

3 FREE Scripts to Instantly
Improve SQL Server Performance

Learn how to increase Performance of SQL Server INSTANTLY with 3 free scripts.