0

复合索引是否足以查询列的子集?

CREATE TABLE [FILE_STATUS_HISTORY]
(
    [FILE_ID] [INT] NOT NULL,
    [STATUS_ID] [INT] NOT NULL,
    [TIMESTAMP_UTC] [DATETIME] NOT NULL,

    CONSTRAINT [PK_FILE_STATUS_HISTORY] 
        PRIMARY KEY CLUSTERED ([FILE_ID] ASC, [STATUS_ID] ASC)
) ON [PRIMARY]

CREATE UNIQUE NONCLUSTERED INDEX [IX_FILE_STATUS_HISTORY] 
ON [FILE_STATUS_HISTORY] ([FILE_ID] ASC,
                          [STATUS_ID] ASC,
                          [TIMESTAMP_UTC] ASC) ON [PRIMARY]
GO


SELECT TOP (1) *
FROM [FILE_STATUS_HISTORY]
WHERE [FILE_ID] = 382748
ORDER BY [TIMESTAMP_UTC] DESC
4

1 回答 1

1

一个复合索引( File_Id, Timestamp_UTC desc )应该优化处理whereand top/order by子句。实际的执行计划会显示查询优化器是否同意。

覆盖索引也将作为Status_Id包含,以便索引可以在单个查找中满足整个查询。

于 2019-12-12T03:15:16.607 回答