SQL Server Performance Tuning

Untrusted Foreign Keys And Constraints Can Degrade Your SQL Server Performance

Updated
4 min read
Written by
Mark Varnas

Disabling 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 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 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” with 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:

Speak with a SQL Expert

In just 30 minutes, we will show you how we can eliminate your SQL Server headaches and provide 
operational peace of mind

Article by
Mark Varnas
Founder | CEO | SQL Veteran
Hey, I'm Mark, one of the guys behind Red9. I make a living performance tuning SQL Servers and making them more stable.

Discover More

SQL Server Health Check SQL Server Migrations & Upgrades SQL Server Performance Tuning SQL Server Security SQL Server Tips

Discover what clients are saying about Red9

Red9 has incredible expertise both in SQL migration and performance tuning.

The biggest benefit has been performance gains and tuning associated with migrating to AWS and a newer version of SQL Server with Always On clustering. Red9 was integral to this process. The deep knowledge of MSSQL and combined experience of Red9 have been a huge asset during a difficult migration. Red9 found inefficient indexes and performance bottlenecks that improved latency by over 400%.

Rich Staats 5 stars
Rich Staats
Cloud Engineer
MetalToad

Always willing to go an extra mile

Working with Red9 DBAs has been a pleasure. They are great team players and have an expert knowledge of SQL Server database administration. And are always willing to go the extra mile to get the project done.
5 stars
Evelyn A.
Sr. Database Administrator

Boosts server health and efficiency for enhanced customer satisfaction

Since adding Red9 to the reporting and DataWarehousing team, Red9 has done a good job coming up to speed on our environments and helping ensure we continue to meet our customer's needs. Red9 has taken ownership of our servers ensuring they remain healthy by monitoring and tuning inefficient queries.
5 stars
Andrew F.
Datawarehousing Manager
See more testimonials