开发者

SQL Server 2008 R2 and execution plan in the indexed view

开发者 https://www.devze.com 2023-01-30 01:54 出处:网络
I\'ve created an indexed view (MyView) that consists of three columns: Table1_ID (int not null) Object_CreationDate (datetime, null)

I've created an indexed view (MyView) that consists of three columns:

Table1_ID (int not null)
Object_CreationDate (datetime, null)
Objec_Count(bigint null)

I have created clustered unique index IX_1 on two columns: Table1_ID And Object_CreationDate

I want to run two queries:

1.

S开发者_StackOverflowelect * from [dbo].MyView
where Table1_ID = 10

2.

 Select * from [dbo].MyView
where Table1_ID = 10
AND Object_CreationDate <= GETDATE()

1-st query runs fast (even with DBCC DROPCLEANBUFFERS()) and use simple execution plan via using MyView and IX_1

2-nd query runs not so fast because it uses "old" execution plan (seeking by several indexes in three tables and nested looping)

I misunderstand this situation. As for me, it is natural use IX_1 And MyView for 2-nd query.

Moreover, I wait that 2-nd query runs the same speed or even faster then 1-st, because it uses two columns in where clause that are in clustered index.

I tried run 2-nd query with(index=IX_1) and update statistics for columns, but still have the same execution plan.

Is it possible to force sql use MyView AND IX_1 ?


Unless you're using Enterprise/Developer edition, you need to include the WITH NOEXPAND hint

 Select * from [dbo].MyView WITH (NOEXPAND)
where Table1_ID = 10
AND Object_CreationDate <= GETDATE()

From Designing Indexed Views:

Indexed views can be created in any edition of SQL Server 2008. In SQL Server 2008 Enterprise, the query optimizer automatically considers the indexed view. To use an indexed view in all other editions, the NOEXPAND table hint must be used.

(And Developer Edition is basically Enterprise Edition, with different licensing)

0

精彩评论

暂无评论...
验证码 换一张
取 消