سؤال

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.

Select * 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)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top