SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find Unindexed Foreign Keys (2005)


Find Unindexed Foreign Keys (2005)

Author
Message
Michael L. Smith
Michael L. Smith
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 38
Comments posted to this topic are about the item Find Unindexed Foreign Keys (2005)
Michael L. Smith
Michael L. Smith
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 38
Time after time, we hear the recommendation, "index your foreign keys." This is why this script was written. Now, the column or columns being referenced must be either a primary key or a unique constraint/index. That leaves referencing column(s) to be indexed...

This script omits statistics and system objects. I want to take this opportunity to address a limitation of the script. The script assumes the order of columns in an appropriate index will be the same order as in the constraint definition, per this join condition:

foreign_key_columns.constraint_column_id = index_columns.key_ordinal

This is not always true! For composite indexes (or indices), we are free to manipulate the key column order. A few guiding principles are:

1. SARGs go leftmost
2. Most selective column goes lefmost
3. Equality columns go leftmost

For more detailed discussion of these points, I recommend these articles: "The Best Indexes for Joins," SQL Server Magazine, May 2002; and "The Big Cover-Up," SQL Server Magazine, September 2001 both by Kalen Delaney.

Thus, you may have a foreign key appropriately indexed -- giving you better performance than an index in FK-definition order -- but still reported by this script...

Is there a reader out there that can formulate another script to allow for different but appropriate order? I'm also looking for a non-cursor based solution to identify potential foreign keys...


Thanks,
Mike
janis.l.murphy
janis.l.murphy
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 127
How about this:

WITH CTE_missingIndexKeyOrdinal AS (
SELECT DISTINCT /* remove dups caused by composite constraints */
DB_NAME() AS [database_name],
OBJECT_NAME(foreign_keys.parent_object_id) AS [table_name],
foreign_keys.[name] AS [fk_name]
FROM sys.foreign_keys AS foreign_keys
JOIN sys.foreign_key_columns AS foreign_key_columns
ON foreign_keys.[object_id] = foreign_key_columns.constraint_object_id
WHERE NOT EXISTS (
SELECT 'An index with same columns and column order'
FROM sys.indexes AS indexes
JOIN sys.index_columns AS index_columns
ON indexes.[object_id] = index_columns.[object_id]
WHERE foreign_keys.parent_object_id = indexes.[object_id]
AND indexes.index_id = index_columns.index_id
AND foreign_key_columns.constraint_column_id = index_columns.key_ordinal
AND foreign_key_columns.parent_column_id = index_columns.column_id
AND OBJECTPROPERTYEX(indexes.[object_id],'IsMSShipped') = 0
AND indexes.is_hypothetical = 0
)

AND foreign_keys.is_ms_shipped = 0

)
, CTE_missingIndexColumniId AS (

SELECT DISTINCT /* remove dups caused by composite constraints */
DB_NAME() AS [database_name],
OBJECT_NAME(foreign_keys.parent_object_id) AS [table_name],
foreign_keys.[name] AS [fk_name]
FROM sys.foreign_keys AS foreign_keys
JOIN sys.foreign_key_columns AS foreign_key_columns
ON foreign_keys.[object_id] = foreign_key_columns.constraint_object_id
WHERE NOT EXISTS (
SELECT 'An index with same columns and [POSSIBLY DIFFERENT] column order'
FROM sys.indexes AS indexes
JOIN sys.index_columns AS index_columns
ON indexes.[object_id] = index_columns.[object_id]
WHERE foreign_keys.parent_object_id = indexes.[object_id]
AND indexes.index_id = index_columns.index_id
AND foreign_key_columns.constraint_column_id = index_columns.index_column_id
AND foreign_key_columns.parent_column_id = index_columns.column_id
AND OBJECTPROPERTYEX(indexes.[object_id],'IsMSShipped') = 0
AND indexes.is_hypothetical = 0
)

AND foreign_keys.is_ms_shipped = 0

)
SELECT [database_name],
[table_name],
[fk_name]
FROM CTE_missingIndexKeyOrdinal
EXCEPT
SELECT [database_name],
[table_name],
[fk_name]
FROM CTE_missingIndexColumniId

UNION

SELECT [database_name],
[table_name],
[fk_name]
FROM CTE_missingIndexColumniId
EXCEPT
SELECT [database_name],
[table_name],
[fk_name]
FROM CTE_missingIndexKeyOrdinal

;


Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16112 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search