Indexed view basic requirements:
- COUNT_BIG() must be included in the field list.
- An unique clustered index must be created.
create view vw_test1
with schemabinding
as
select cust_id, total_amt = sum(amt), count = count_big(*)
from invoice
group by cust_id
go
create unique clustered index ix_vw_test1
on vw_test1 (cust_id)
go
select *
from vw_test1 with (noexpand)
Notes:
- "WITH (NOEXPAND)" option must be used when you are querying the data through the indexed view. Otherwise, the query optimizer will take use the view definition to query the data and the indexes created for the view will not be used.
References:
http://technet.microsoft.com/en-us/library/ms191432.aspx
http://technet.microsoft.com/en-us/library/cc917715.aspx