Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Generate Create FK-indexes

By Johan Bijnens,

Our standard is to always create foreign key indexes.
These indexes exactly match the columns of the DRI-definition.
This is to avoid scans when parent rows are being deleted.

Stored procedure [Spc_ALZDBA_Create_FK_Ix] will only script the "CREATE INDEX" statements.
You'll have to copy/paste and execute yourself, based on your own preferences.

This select statement will generate exec-statements for all base tables.
Copy/paste and execute or declare a cursor and execute immediate.

-- Execution example
select 'exec Spc_ALZDBA_Create_FK_Ix @ParentTbName = '''+ T.TABLE_NAME + ''' ,@ParentTbOwner = ''' + T.TABLE_SCHEMA + ''', @CheckExistIx = 1 ' + char(10) + 'go'
from INFORMATION_SCHEMA.Tables T
where T.TABLE_TYPE = 'BASE TABLE'
order by T.TABLE_SCHEMA, T.TABLE_NAME

-- Execution example for the procedure
exec Spc_ALZDBA_Create_FK_Ix @ParentTbName = 'Address' ,@ParentTbOwner = 'Person', @CheckExistIx = 1

-- result:
Create index [XFK_ALZ_FK_EmployeeAddress_Address_AddressID] on [HumanResources].[EmployeeAddress] ( AddressID ) ;
GO

This procedure will only script the "CREATE INDEX" statements.
You'll have to copy/paste and execute yourself, based on your own preferences.

TEST IT - TEST IT - TEST IT - TEST IT

Total article views: 1362 | Views in the last 30 days: 6
 
Related Articles
SCRIPT

Scripting CREATE INDEX Statements Automatically From Your Tables

Stored proc for scripting out full CREATE INDEX statements from your table indexes with all filegrou...

FORUM

Tips on creating indexes

Tips on creating indexes

FORUM

Dynamically Creating Indexes

Dynamically Creating Indexes

FORUM

Execution Plan -- Clustered Index Update

Execution Plan -- Clustered Index Update

FORUM

How to execute Index?

How to execute Index?

Tags
indexing    
 
Contribute