Subscribe | Alerts via Email
View All Quotes
“In an ideal world, every system could run instantly, consume zero storage space, use zero network bandwidth, never contain any errors, and cost nothing to build. In the real world, a key part of the designer's job is to weigh competing design characteristics and strike a balance among those characteristics.”
-Steve McConnell
<February 2010>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

©2010 Cal Zant
Sign In
Total Posts: 103
This Year: 2
This Month: 0
This Week: 0
Comments: 2

In my recent post on How To: Find Missing Indexes in SQL Server, I showed how to find the information related to indexes that SQL Server thinks would be helpful (based on query stats it has been keeping since it was last rebooted).  The actual "CREATE INDEX" script you have to write based on that info can be a little tedious to get write, so I created a user-defined function that you could simply pass some of the info you get from the query in that post and it will return the necessary T-SQL you would need to run in order to create the index for the given info.

ALTER FUNCTION [dbo].[bcz_fn_GetCreateIndexScript](@TableName varchar(100), @EqualityColumns varchar(500), @InequalityColumns varchar(500), @IncludedColumns varchar(500))
RETURNS varchar(MAX)
BEGIN

 DECLARE @CreateScript varchar(MAX), @Name varchar(128), @EqualityAndInequalityColumnList varchar(MAX), @ColumnListForName varchar(MAX), @MaxColumnListLength int, @IndexPrefix varchar(63)

 -- Construct what the prefix of the index will be (this will be followed by the list of related columns)
 SET @IndexPrefix = 'IX_' + @TableName + '_'

 -- Construct a list of all columns that will be involved with the index (equality, inequality, and included)
 SET @EqualityAndInequalityColumnList = ''
 IF @EqualityColumns IS NOT NULL
  SET @EqualityAndInequalityColumnList = @EqualityColumns
 
 IF @InequalityColumns IS NOT NULL
 BEGIN
  IF (LEN(@EqualityAndInequalityColumnList) > 0) SET @EqualityAndInequalityColumnList = @EqualityAndInequalityColumnList + ', ' + @InequalityColumns
  ELSE SET @EqualityAndInequalityColumnList = @InequalityColumns
 END
 
 SET @ColumnListForName = @EqualityAndInequalityColumnList
 
 IF @IncludedColumns IS NOT NULL
 BEGIN
  IF (LEN(@ColumnListForName) > 0) SET @ColumnListForName = @ColumnListForName + ', ' + @IncludedColumns
  ELSE SET @ColumnListForName = @IncludedColumns
 END

 -- Remove the brackets around the column names, and make it "_" delimited instead of ", " delimited
 SET @ColumnListForName = REPLACE(REPLACE(REPLACE(@ColumnListForName, '[', ''), ']', ''), ', ', '_')
 
 -- The max length of the index name must be <= 128 ... so calculate how big the column list can be with
 -- respect to that and the given naming convention we will use.
 SET @MaxColumnListLength = 128 - LEN(@IndexPrefix)

 -- Create the name with the list of columns appended to the end of it
 SET @Name = @IndexPrefix + SUBSTRING(@ColumnListForName, 1, @MaxColumnListLength)

 SET @CreateScript =
  'CREATE NONCLUSTERED INDEX ' + @Name + ' ON ' + @TableName + ' (' + @EqualityAndInequalityColumnList + ')'
  
 IF @IncludedColumns IS NOT NULL
  SET @CreateScript = @CreateScript + ' INCLUDE (' + @IncludedColumns + ')'

 RETURN @CreateScript
END

If you would like to dig deeper into this, here is a good resource: http://msdn.microsoft.com/en-us/library/ms345405.aspx.

Wednesday, July 08, 2009 1:29:35 PM (Central Standard Time, UTC-06:00)  #