我正在尝试调查每个索引的页数,并制定了以下查询:
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
并填充100,000行随机数据。
我希望看到两个条目:唯一约束的非聚集索引和主键列的聚簇索引。
这些双重条目是什么意思?
答案 0 :(得分:1)
这意味着您的JOIN
条件错误了 - 您还需要在index_id
条件中加入JOIN
- 就像这样:
INNER JOIN
sys.indexes as i ON pyi.object_id = i.object_id
AND pyi.index_id = i.index_id