2014-09-29 44 views
0

我試圖調查到的每個索引頁面的數量,並制定了以下查詢:SQL Server 2008的:在一個索引的網頁數量

SELECT    
     pyi.index_type_desc as [index type], 
     i.name as [index name], 
     sum(pyi.page_count) as [number of pages] 
FROM 
    sys.dm_db_index_physical_stats(DB_ID('testdb'),OBJECT_ID('surrogatekeys'),NULL,NULL,'DETAILED') as pyi 
INNER JOIN 
    sys.indexes as i ON pyi.object_id = i.object_id  
GROUP BY 
    i.name, pyi.index_type_desc 

當我運行此查詢,我得到以下結果:

CLUSTERED INDEX PK_TableWithSurrogateKeyAndUniqueContraints 14342 
CLUSTERED INDEX UNIQUE_TableWithSurrogateKeyAndUniqueContraints 14342 
NONCLUSTERED INDEX PK_TableWithSurrogateKeyAndUniqueContraints 707 
NONCLUSTERED INDEX UNIQUE_TableWithSurrogateKeyAndUniqueContraints 707 

此表的創建如下:

CREATE TABLE [dbo].[SurrogateKeys] 
(
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [TimeStamp] [datetime2](7) NOT NULL, 
    [Username] [nvarchar](20) NOT NULL, 
    [Message] [nvarchar](500) NULL, 

    CONSTRAINT [PK_TableWithSurrogateKeyAndUniqueContraints] 
     PRIMARY KEY CLUSTERED ([Id]), 
    CONSTRAINT [UNIQUE_TableWithSurrogateKeyAndUniqueContraints] 
     UNIQUE ([Username], [TimeStamp]), 
) ON [PRIMARY] 
GO 

,並擁有10萬行Ø填充f隨機數據。

我期望看到兩個條目:唯一約束的非聚集索引和主鍵列的聚集索引。

這些雙項是什麼意思?

回答

1

這意味着你得到了JOIN條件錯了 - 你還需要包括index_idJOIN狀態 - 就像這樣:

INNER JOIN 
    sys.indexes as i ON pyi.object_id = i.object_id 
         AND pyi.index_id = i.index_id 
+0

這做到了感謝。 – 2014-09-29 13:36:24

+0

一個簡單的問題,也許是一個愚蠢的問題:如果來自sys.dm_db_index_physical_stats的第一個查詢返回6行,爲什麼連接只返回4行?我會期待它返回不完整連接中的所有6行。 – 2014-09-29 13:51:18