For example, if we have a table Employees (EmpID, EmpName), then will SQL Server execute the following commands successfully?
Create clustered index indx1 on Employees (EmpID)
Go
Create nonclustered index indx2 on Employees (EmpID)
Go
yes, it is possible but it is not clear why you would want to do that. Here is the output of sp_help on the table empoyee and you will notice that there are two indexes one clustered and other non-clustered on the same key.
Name Owner Type Created_datetime
-- -- - --
Employees dbo user table 2005-10-28 09:30:42.733
Column_name Type Computed Length Prec Scale Nullable TrimTrailingBlanks FixedLenNullInSource Collation
-- -- -- -- -- -- -- -- -- --
EmpID int no 4 10 0 yes (n/a) (n/a) NULL
EmpName varchar no 100 yes no yes SQL_Latin1_General_CP1_CI_AS
Identity Seed Increment Not For Replication
-- -
No identity column defined. NULL NULL NULL
RowGuidCol
--
No rowguidcol column defined.
Data_located_on_filegroup
--
PRIMARY
index_name index_description index_keys
-- -
indx1 clustered located on PRIMARY EmpID
indx2 nonclustered located on PRIMARY EmpID
No constraints are defined on object 'employees', or you do not have permissions.
No foreign keys reference table 'employees', or you do not have permissions on referencing tables.
No views with schema binding reference table 'employees'.
|||Well, I was asked this question in an interview :)Even I am not aware about any practical aspect of the question.
Thank you for your response, anyway.|||Thinking more about it, I think it can be useful under following scenarios
(1) you have range queries on empid that retrieves large number of columns in the table so it may make sense to have clustered index on empid.
AND
(2) you have range query on empid where you need only a subset of columns. you can make those subset of columns as included columns such that your query is covered by the index. This will eliminate access to the data page.
thanks
No comments:
Post a Comment