LKBEN11463: How to find untrusted foreign keys in SQL Server.


Symptom

Untrusted foreigh keys can be a performance bottleneck because the query optimizer cannot relay on this key.

Cause

This can come from large bulk loads when you disable the foreign keys and forget to enable them again.

Solution

Go to the database you need to check and select from the sys.foreign_keys.

e.g. Here I put the is_not_trusted in the beginning so you can find it instantly.

select is_not_trusted, * from sys.foreign_keys

Normally you would expect 0. When you have 1 in this column, the query optimizer cannot trust the data and will need to perform extra checks to make shure the date is good. This will lead to bad performance.

You can re-enable the foreign key constraints with the alter table statement.

alter table <name> with check check constraint <foreign_key>

You want your foreign keys to be trusted so the query optimizer can create efficient query plans.

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!

About the Author

Author: Wim Peeters - Keskon GmbH & Co. KG

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.

Latest update: 30.10.2020 | Comment: