Average 28000 requests/month












 

This is a script that I've created in SQL Server 2005 to create indexes on all FKs. Nice use of the new database catalog.

 

select 'create nonclustered index NC_TESA_FK_INDEX_' + ColsA.name + ' on dbo.' + object_name(FKCols.parent_object_id) + '(' + ColsA.name + ')', ColsA.name as FKColumnName, ColsB.name as PKColumnName, 
object_name(FKCols.referenced_object_id) as PKTableName, 
object_name(FKCols.parent_object_id) as FKTableName
from sys.foreign_key_columns FKCols 
inner join sys.columns ColsA
on FKCols.parent_object_id = ColsA.[object_id]
and FKCols.parent_column_id = ColsA.Column_id
inner join sys.columns ColsB
on FKCols.referenced_object_id = ColsB.[object_id]
and FKCols.referenced_column_id = ColsB.Column_id
where 
not exists
(
select * from sys.indexes indx
inner join sys.index_columns indxCols
on indx.[object_id] = indxCols.[object_id]
and indx.[index_id] = indxCols.[index_id]
inner join sys.columns cols
on indxCols.Column_id = cols.Column_id
and indxCols.[object_id] = cols.[object_id]
where indxCols.Column_id = FKCols.parent_column_id
and indxCols.[object_id] = FKCols.parent_object_id
and indx.type_desc = 'nonclustered'
)


 
 

Dec 2005