Question

I'm using SQL Server 2000, I hesitate to start using indexed views (I've a table with daily performance values, and I need to score them with many mathematical function).

If I create an indexed view (using my performance table), and then I add a new line into my performance table, is my view's index immediately updated, or is it updated at the first user request on the view?

Était-ce utile?

La solution

Indexed views are maintained automatically as part of the queries that affect the base tables on which they are based.

This is why there are so many restrictions on what you can have in an indexed view - the restrictions exist so that the view can be updated just based on the rows affected in the base table, rather than (potentially) having to re-scan the entire table(s) to determine what rows the view should now contain.


You can also see this by examining the query plan of the INSERT for the following queries:

create table dbo.T (ID int not null)
go
create view dbo.V 
with schemabinding
as
    select ID from dbo.T
go
create unique clustered index I on dbo.V(ID)
go
insert into T(ID) values (1)

And the plan for insert into T(ID) values (1) is:

enter image description here

Where as you can see, the plan includes an insert into the index I on view V.


The above was done on a newer version of SQL Server than 2000 (I don't keep unsupported versions lurking around), but it is the way these have always worked. Even back in the 2000 version of the documentation, this restriction is mentioned:

After the clustered index is created, any connection attempting to modify the base data for the view must also have the same option settings required to create the index. SQL Server generates an error and rolls back any INSERT, UPDATE, or DELETE statement that will affect the result set of the view if the connection executing the statement does not have the proper option settings.

If the view was only updated when it was accessed, there would be no need for this restriction to exist.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top