LKBEN11465: How to check for unstrusted foreign keys in MS Sql Server


This article has not been checked!

LKB | Created: 12/11/2020 | Version: 0 | Language: EN | Rating: 0 | Outdated: False | Marked for deletion: False

Author: Wim Peeters - Keskon GmbH & Co. KG

Latest update: 12/11/2020 | Comment:


Symptom

Your performance is suffering and you want to check if your constraints are still trusted

Cause

This can be the case with import scripts. A script can disable the checks, import and than enable the checks again. If something goes wrong...

Solution

Use use the following query on your database to find information about your foreign keys. The "is_not_trusted" should be 0. When this is not the case, you have a problem and should correct it. Untrusted foreign keys can cause performance problems because the query analyser has to build the checks into the query plan.

select name, type_desc, is_not_trusted from sys.foreign_keys

To correct this, you need to alter the table.

alter table table_name with check check constraint constraint_name

Have fun.

 

About the Author

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience, including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform.

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!