SqlServer 2012 - Column '' in table '' is of a type that is invalid for use as a key column in an index

2

After seeing this error when creating a NONCLUSTERED index I wondered if it would really be necessary to create this index in the following scenario:

I have a table with 11 columns, a column I store a URL, ie it is a column nvarchar(300) , this column accepts nulls.

At some point I need to do a SQL to get the table values where that column is not null. So I wanted to create an index to speed it up.

I'm using EF6 and SQLServer 2012. Is there a gain in creating an index in this scenario? If it exists how would I create it?

    
asked by anonymous 14.05.2015 / 22:59

1 answer

2

This index would not be useful for this type of query you described.

  

We should not create indexes for columns whose information varies very little between records.

For example, it does not make sense to create an index for a column that always contains either zero or one. It is unlikely that an index like this will be used by the bank because a table scan (scrolling across the entire table instead of using the index) would most often be more efficient.

As your query checks only whether or not the column is null, what is being checked is if it is in one of only two possible states, that is, as in the example of zeros and ums, it is very it is unlikely that this index will be used since table scan will generally perform better.

  

Indices that are not used in the queries should not be created because although they do not bring any benefits they still need to be maintained by the database, ie they occupy space and consume processing.

See this Microsoft article for very interesting general guidelines on indexing design : SQL Server Index Design Guide .

  Finally, although there are useful general guidelines, it is always good to analyze the query execution plan to identify whether or not an index should be created or removed in a particular scenario.

    
14.05.2015 / 23:53