title: Microsoft SQL Server (MSSQL) updatable view with multiple base tables and full performance
body: <p>I am using MSSQL 2008 R2. It has the handy feature of updatable views. For example if I have one table t mapping id to name:</p>
  
  <pre><code>create table t (id int not null primary key, name varchar(100) not null unique)
  </code></pre>
  
  <p>and then another table giving some ids and some more information:</p>
  
  <pre><code>create table u (id int not null primary key references t, info varchar(100) not null)
  </code></pre>
  
  <p>and for convenience, a view that lets me see rows from u augmented with the name column:</p>
  
  <pre><code>create view v as select u.*, t.name from u u join t t on u.id = t.id
  </code></pre>
  
  <p>then I can now do updates by name, rather than id:</p>
  
  <pre><code>update v set info = 'foo' where name = 'fred'
  </code></pre>
  
  <p>Most handy.  But what happens if I wanted to delete the row for 'fred'?</p>
  
  <pre><code>delete v where name = 'fred' -- Fails
  </code></pre>
  
  <p>I get the error</p>
  
  <blockquote>
    <p>View or function 'v' is not updatable because the modification affects multiple base tables.</p>
  </blockquote>
  
  <p>As explained in <a href="https://stackoverflow.com/questions/7281054/">SQL Updatable View with joined tables</a> (which refers to Oracle, but the situation appears the same for MSSQL) you can have an updatable view over more than one base table, as long as there is only one <em>key-preserved</em> table; roughly speaking this is where any row from the table appears at most once in the view.  In the above view we can see that both t and u are key-preserved tables.  But we can cheat by tweaking the view definition:</p>
  
  <pre><code>create view v as
  select u.*, (select t.name from t t where t.id = u.id) as name
  from u u
  </code></pre>
  
  <p>This gives the same rows as before, but now allows updates:</p>
  
  <pre><code>update v set info = 'foo' where name = 'fred'
  </code></pre>
  
  <p>Semantically, it remains true that any row from t appears at most once in the view, but because we have not joined to t in the normal way we don't hit the update restriction.  Moreover we can also delete from this view:</p>
  
  <pre><code>delete from v where name = 'fred'
  </code></pre>
  
  <p>This does the right thing, deleting from the underlying table u but not from t.  Clearly, with the earlier view expressed as a simple join, there would be no way to tell whether the 'delete' operation should remove the row from u or from t (or both).</p>
  
  <p>For many 'select' queries, the execution plan using the rewritten view is a bit different, so I might expect it to perform a bit slower in some cases.  It is a shame the optimizer isn't able to see that (in this particular case, with the unique indexes that exist) the two views have the same data.</p>
  
  <p>You can also make an updatable view using a function:</p>
  
  <pre><code>create function dbo.get_name(@id int) returns varchar(100) as begin
    declare @r varchar(100)
    select @r = name from t where id = @id
    return @r
  end
  
  create view v as select *, dbo.get_name(id) as name from u
  </code></pre>
  
  <p>This can give still different (and often more complex) query plans, so it might be slower still.</p>
  
  <p>So we have two possible ways to make an updatable view, but they're not entirely satisfactory.  It would be nice to have the update and delete operations working but yet be certain that the view won't perform any worse on select queries than the simple join of two tables; perhaps there is some hint you can give to the query engine.  Could anyone suggest something?</p>