I am doing some analysis and trying to gather some understanding of unique and non-unique non clustered indexes on SQL Server 2008 ? Following a technical blog,I did the following.
USE TEST
CREATE TABLE "CustomersHeap" (
"CustomerId" INT NOT NULL,
"CustomerName" CHAR(100) NOT NULL,
"CustomerAddress" CHAR(100) NOT NULL,
"Comments" CHAR(189) NOT NULL
)
;
USE TEST
DECLARE @i INT = 1 WHILE (@i <= 80000) BEGIN
INSERT INTO dbo.CustomersHeap VALUES
(
@i,
'CustomerName' + CAST(@i AS CHAR),
'CustomerAddress' + CAST(@i AS CHAR),
'Comments' + CAST(@i AS CHAR)
) SET @i += 1
END
-- Create a unique non clustered index
CREATE UNIQUE NONCLUSTERED INDEX IDX_UniqueNCI_CustomerID
ON Test.dbo.CustomersHeap(CustomerId)
-- Create a non-unique non clustered index
CREATE NONCLUSTERED INDEX IDX_NonUniqueNCI_CustomerID
ON Test.dbo.CustomersHeap(CustomerId)
--Get indexes
select * from sys.indexes where object_id=OBJECT_ID('Test.dbo.CustomersHeap')
--After figuring out index and child pages using DBCC IND
DBCC TRACEON(3604)
--UniqueNonClusteredIndex root page
DBCC PAGE(Test, 1, 4160, 3)
--NonUniqueNonClusteredIndex root page
DBCC PAGE(Test, 1, 4416, 3)
The output of first page statement does not contain a column called 'HEAP RID' where as the output of second page statement does. I am unable to understand the reason why ? especially when all the rows have information of ChildPageId and they should contain 'HEAP RID' rightfully.
This additional columns adds to the size of the root page of Non-Unique Non Clustered Index.
Output columns for first page statement FileId,PageId,Row,Level,ChildFileId,ChildPageId,CustomerId,KeyHashValue
Output columns for second page statement FileId,PageId,Row,Level,ChildFileId,ChildPageId,CustomerId,HEAP RID,KeyHashValue