Yes, it is redundant.
On the other hand, it won't hurt anything (since the optimizer will ignore it and not add the data twice).
This is one of the many reasons not to take the suggested indexes at face value.
Computed Columns can be stored in the data page in one of two ways. Either by creating them as PERSISTED
or by including them in the clustered index definition.
If they are included in the clustered index definition then even if the columns are not marked as PERSISTED
then the values will still be stored in each row. These index key values will additionally be stored in the upper level pages.
If the computed column is imprecise (e.g. float
) or not verifiable as deterministic (e.g. CLR functions) then it is a requirement for the column to be marked as PERSISTED
in order to be made part of an index key.
So to give an example
CREATE TABLE T
(
A INT,
C1 AS REPLICATE(CHAR(A),100) PERSISTED,
C2 AS REPLICATE(CHAR(A),200),
C3 AS CAST(A AS FLOAT) PERSISTED,
C4 AS CAST(A + 1 AS FLOAT)
)
CREATE UNIQUE CLUSTERED INDEX IX ON T(C2,C3)
C1
will be stored in just the data page rows as it is marked as
PERSISTED
but not indexed.
C2
will be stored in both the rows on the data page and the index higher levels as it is an index key column.
C3
will be stored as for C2
. As it is imprecise
it is a requirement to mark it as PERSISTED
however.
C4
won't be stored anywhere as it is neither marked as PERSISTED
nor indexed.
Similarly all computed columns referenced in non clustered index definitions as key columns need to be stored at all levels of the index as they are part of the index key. There is the same requirement regarding precise/deterministic results.
Fails
CREATE NONCLUSTERED INDEX IX2 ON T(A,C4)
With the error.
Cannot create index or statistics 'IX2' on table 'T' because the
computed column 'C4' is imprecise and not persisted. Consider removing
column from index or statistics key or marking computed column
persisted.
To include it as part of the non clustered index key it must also be stored in the clustered index data pages. However
Succeeds.
CREATE NONCLUSTERED INDEX IX2 ON T(A) INCLUDE (C4)
Computed columns that are only INCLUDE
d columns are persisted to the NCI leaf page and do not have the requirement that they also be persisted in the data page.
Best Answer
Order of your data returned is not guaranteed when you do not specify an order by.
Consider this example:
If you were to just select without an order by:
The order this time is the same as your first example:
But this is not guaranteed.
If you want the ordering to be guaranteed then you need to add an order by:
What you are seeing when creating the nonclustered index on number is the query using this new NC index.
For example:
Running the query again:
I get the same result as you:
Because now sql server decides to use the
IX_Number
index to return the results:Which is ordered by
number asc
.To guarantee the ordering the query needs to be changed to:
The ordering could even be different than the index order from the index that was used. This means that even if now the ordering is on the
number
orname
column, it could be on any one of these columns or even none.Always use an
order by
if the ordering is important.More info on the subject can be found here, here, here and here