sqlsql-servert-sqlviewindexed-view

Updating underlying tables of indexed view but the column is not present in the view


Let's say I have two tables, Country and City.

Country(id, fname, president_name)
City(id, cname, country_id_fk, mayor_name)

City table has a foreign key dependency on the Country table. Let's say I create a indexed view over tables like this:

CREATE VIEW CountryCity
WITH SCHEMABINDING
AS
    SELECT Country.fname, City.cname
    FROM Country INNER JOIN City
    ON Country.id = City.country_id_fk;
GO

And create a unique clustered index over that view

CREATE UNIQUE CLUSTERED INDEX ucidx_cc
ON CountryCity(cname, fname);
GO

Note that I do not have president_name in the view. If I update the president_name in Country table, will that affect the CountryCity view.

What I mean to say be affect is will there be performance issues during updates in Country table because we have a indexed view over that table?


Solution

  • You can basically have an index with particular columns. When you are updating other columns, if they are not included in the index, no operations for maintaining its data are applied.

    Also, the SQL Engine is able to maintain a lot of indexes per table, so there is no need to worry for such simple design.