且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server索引视图

更新时间:2022-05-24 22:32:39

视图将只使用表索引,除非 NOEXPAND 提示(文档此处 )。

The view will simply utilize the table index unless the NOEXPAND hint is supplied (documentation here).

您可以按照以下方式自行测试:

You can test this yourself as follows:

CREATE TABLE [test].[TestTable] (
    id INT IDENTITY PRIMARY KEY,
    foo INT
)

CREATE NONCLUSTERED INDEX ixFoo
ON [test].[TestTable] (foo)

CREATE VIEW [test].[TestTableView] WITH SCHEMABINDING
AS
    SELECT
        t.id,
        t.foo
    FROM [test].[TestTable] t
GO

CREATE UNIQUE CLUSTERED INDEX ixFooId
ON [test].[TestTableView] (id)

CREATE NONCLUSTERED INDEX ixFooView
ON [test].[TestTableView] (foo)

以下是三个单独查询的执行计划:

Here's the execution plan for three separate queries:

SELECT
    t.[id],
    t.[foo]
FROM [test].[TestTable] t
ORDER BY t.[foo]

SELECT
    v.[id],
    v.[foo]
FROM [test].[TestTableView] v
ORDER BY v.[foo]

SELECT
    v.[id],
    v.[foo]
FROM [test].[TestTableView] v WITH (NOEXPAND)
ORDER BY v.[foo]