0

I have an indexed view FooView.

I've created the following indexes against it:

  • CREATE UNIQUE CLUSTERED INDEX IX_Foo1 ON [FooView](SomeId, AnotherId)
  • CREATE NONCLUSTERED INDEX IX_Foo2 ON [FooView](SomeId)

Is it possible to use a HINT against IX_Foo2 ? It keeps using IX_Foo1 when I use the WITH (NOEXPAND) hint.

4

1 回答 1

2

是的,这非常简单

CREATE TABLE dbo.FooTable
  (
     SomeId    INT,
     AnotherId INT,
     Filler    CHAR(8000)
  );

go

CREATE VIEW dbo.FooView
WITH SCHEMABINDING
AS
  SELECT SomeId,
         AnotherId,
         Filler
  FROM   dbo.FooTable

GO

CREATE UNIQUE CLUSTERED INDEX IX_Foo1
  ON dbo.FooView(SomeId, AnotherId)

CREATE NONCLUSTERED INDEX IX_Foo2
  ON dbo.FooView(SomeId)

GO

SELECT SomeId
FROM   dbo.FooView WITH (noexpand) --No hint non clustered index chosen automatically

SELECT *
FROM   dbo.FooView WITH (noexpand) --No hint clustered index chosen automatically

SELECT *
FROM   dbo.FooView WITH (noexpand, INDEX = IX_Foo2) --With hint nonclustered index forced

执行计划

(请注意,使用提示强制索引会导致比将选择留给优化器更昂贵的计划)

在此处输入图像描述

于 2015-03-18T21:00:51.130 回答