/*
One or more tables found, with foreign key constraint defined but no supporting indexes created on the foreign key columns.
SQL Server doesnt put an index on foreign key columns by default and indexing foreign key fields in referencing tables is not required.
Foreign key columns usage must evaluated to determine whether or not indexing this column will help up increase the current
workloads performance by enhancing join performance, reducing table locking (for full table scans) while cascading updates and
deletes, etc.
*/
;
WITH FKTable
AS ( SELECT SCHEMA_NAME(po.schema_id) AS 'parent_schema_name' ,
OBJECT_NAME(fkc.parent_object_id) AS 'parent_table_name' ,
OBJECT_NAME(constraint_object_id) AS 'constraint_name' ,
SCHEMA_NAME(ro.schema_id) AS 'referenced_schema' ,
OBJECT_NAME(referenced_object_id) AS 'referenced_table_name' ,
( SELECT '[' + COL_NAME(k.parent_object_id,
parent_column_id) + ']' AS [data()]
FROM sys.foreign_key_columns (NOLOCK) AS k
INNER JOIN sys.foreign_keys (NOLOCK) ON k.constraint_object_id = object_id
AND k.constraint_object_id = fkc.constraint_object_id
ORDER BY constraint_column_id
FOR
XML PATH('')
) AS 'parent_colums' ,
( SELECT '[' + COL_NAME(k.referenced_object_id,
referenced_column_id) + ']' AS [data()]
FROM sys.foreign_key_columns (NOLOCK) AS k
INNER JOIN sys.foreign_keys (NOLOCK) ON k.constraint_object_id = object_id
AND k.constraint_object_id = fkc.constraint_object_id
ORDER BY constraint_column_id
FOR
XML PATH('')
) AS 'referenced_columns'
FROM sys.foreign_key_columns fkc ( NOLOCK )
INNER JOIN sys.objects po ( NOLOCK ) ON fkc.parent_object_id = po.object_id
INNER JOIN sys.objects ro ( NOLOCK ) ON fkc.referenced_object_id = ro.object_id
WHERE po.type = 'U'
AND ro.type = 'U'
GROUP BY po.schema_id ,
ro.schema_id ,
fkc.parent_object_id ,
constraint_object_id ,
referenced_object_id
),
/* Index Columns */
IndexColumnsTable
AS ( SELECT SCHEMA_NAME(o.schema_id) AS 'schema_name' ,
OBJECT_NAME(o.object_id) AS TableName ,
( SELECT CASE key_ordinal
WHEN 0 THEN NULL
ELSE '[' + COL_NAME(k.object_id,
column_id) + ']'
END AS [data()]
FROM sys.index_columns (NOLOCK) AS k
WHERE k.object_id = i.object_id
AND k.index_id = i.index_id
ORDER BY key_ordinal ,
column_id
FOR
XML PATH('')
) AS cols
FROM sys.indexes (NOLOCK) AS i
INNER JOIN sys.objects o ( NOLOCK ) ON i.object_id = o.object_id
INNER JOIN sys.index_columns ic ( NOLOCK ) ON ic.object_id = i.object_id
AND ic.index_id = i.index_id
INNER JOIN sys.columns c ( NOLOCK ) ON c.object_id = ic.object_id
AND c.column_id = ic.column_id
WHERE o.type = 'U'
AND i.index_id > 0
GROUP BY o.schema_id ,
o.object_id ,
i.object_id ,
i.name ,
i.index_id ,
i.type
),
FKWithoutIndexTable
AS ( SELECT fk.parent_schema_name AS SchemaName ,
fk.parent_table_name AS TableName ,
fk.referenced_schema AS ReferencedSchemaName ,
fk.referenced_table_name AS ReferencedTableName ,
fk.constraint_name AS ConstraintName ,
fk.referenced_columns AS Referenced_Columns ,
fk.parent_colums AS Parent_Columns
FROM FKTable fk
WHERE NOT EXISTS ( SELECT 1
FROM IndexColumnsTable ict
WHERE fk.parent_schema_name = ict.schema_name
AND fk.parent_table_name = ict.TableName
AND fk.parent_colums = LEFT(ict.cols,
LEN(fk.parent_colums)) )
)
SELECT @@SERVERNAME AS InstanceName ,
DB_NAME() AS DatabaseName ,
SchemaName ,
TableName ,
Parent_Columns ,
ReferencedSchemaName ,
ReferencedTableName ,
Referenced_Columns ,
ConstraintName
INTO #ForeignKeyWithOutIndex
FROM FKWithoutIndexTable
ORDER BY DatabaseName ,
SchemaName ,
TableName;
--输出临时表数据
SELECT *
FROM #ForeignKeyWithOutIndex;
--生成外键字段缺少的索引,请抽查、检验,确认后批量执行
SELECT 'CREATE INDEX IX_' + LTRIM(RTRIM(TableName)) + '_'
+ SUBSTRING(Parent_Columns, 2, LEN(Parent_Columns) - 2) + ' ON '
+ LTRIM(RTRIM(SchemaName)) + '.' + LTRIM(RTRIM(TableName)) + '('
+ Parent_Columns + ');'
FROM #ForeignKeyWithOutIndex;
--删除临时表
DROP TABLE #ForeignKeyWithOutIndex;