14

Is it possible to alter an existing non clustered index to include more columns as a part of Covered columns.

e.g.

ALTER INDEX IX_NC_TableName_ColumnName
FOR TableName(ColumnName)
INCLUDE(Col1, Col2, Col3)

Want to include Col4 in above index.

What will be the impact of adding this column? Will there be fragmentation or anything else?

3
  • 5
    There is no option to add a column in an existing index. you will need to drop and recreate. ALTER INDEX.
    – ughai
    Commented Apr 29, 2015 at 11:48
  • @ughai: Is it applicable for Covered columns as well? Commented Apr 29, 2015 at 11:50
  • yes, even covering columns as well
    – ughai
    Commented Apr 29, 2015 at 11:51

1 Answer 1

36

The cost of an additional included column will be increased storage and potentially fragmentation. Fragmentation will increase slightly compared to the old index due to the increased leaf node size (assuming keys are not incremental) and if updates to the new included column increases length.

Consider using CREATE INDEX...WITH DROP EXISTING to this task. This will avoid dropping the old index and avoid a sort, leverage the existing index key sequence for the rebuild:

CREATE INDEX IX_NC_TableName_ColumnName
ON TableName(ColumnName)
INCLUDE(Col1, Col2, Col3, Col4)
WITH(DROP_EXISTING = ON);
2
  • What if the query runs in a batch? In that case, it would drop the index and re-create it each time. So, how to check if the newly added column is in the INCLUDE list or not?
    – yuv
    Commented Jan 12 at 13:36
  • @yuv, one typically applies schema/index changes as part of a release process rather than in code that runs every batch execution. Meta data for included and key columns are exposed in the sys.index_columns catalog view but you would need to check the entire index definition if you want an idempotent script.
    – Dan Guzman
    Commented Jan 12 at 14:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.