2

I want to create an indexed view that integrates data from several tables, however SQL Server complains with the error message:

Cannot create index on view "MyView". The view contains a self join on "dbo.Companies".

The view definition is something like this (simplified version):

SELECT  T.Field1
      , T.Field2
      , P.CompanyName AS ProviderName
      , C.CompanyName AS CustomerName
      , T3.Field1
FROM dbo.Table1 T 
                  INNER JOIN dbo.Companies P ON T.ProviderId = T2.Id
                  INNER JOIN dbo.Companies C ON T.CustomerId = T2.Id
                  INNER JOIN dbo.Table3 ON T.Id = T3.Id

Is there any workaround for this case? I don't want to split the Companies table in two tables.

Thanks in advance.

2 Answers 2

3

You won't be able to work around this, the indexed views must conform to certain restrictions enumerated in Creating Indexed Views. Among other things, outer and self joins are not supported (10th restriction from top to bottom). It boils down to the engine ability to be able to update the view index when the base table is updated.

Not knowing exact all the details of your data model, are you sure that an indexed view is necessary and won't the base table indexes suffice?

9
  • Thanks for your answer. I'm using the base tables now, but I wanted to simplify my queries and get a performance boost as a bonus. One of the lists my app shows to users, needs to join multiple tables (4 or 5), do some column concatenations (surname + name) and it also has a couple of 'case' statements. I think it makes the perfect case for a view (indexed if possible), but this restriction is on my way.
    – JAG
    Jun 18, 2009 at 13:28
  • Well those arguments are not very strong for an indexed view. Surname+name can be a persisted computed indexed column in the base table if you need to seek on it, and most likely the CASEs too. The joins could make a good case for an indexed view, depending on several factors, but then you can always separate the non-self join joined tables into a view and index that, then reference the already indexed view into your final view to benefit the index seeks on it. Again, is more of a guess from me w/o all the info. Jun 18, 2009 at 13:44
  • Even if I use computed persisted columns to avoid the column concatenations I still need to join the main table with those that contains the computed columns. Also note that I have not a self-referencing table, I just reference the same table twice in a query. The SQL Server error is confusing and not exact. I'm going to take your advice and go with two views. If I understand you correctly, you propose to create an indexed view with out one of the tables and the create another view joining the remaining table, right? Thanks for your feedback, I really appreciate it.
    – JAG
    Jun 18, 2009 at 14:16
  • 1
    The problem with EE is this: the Query Optimizer will ignore the indexed view as an option, even when selecting from the view on any edition under EE. The only way to use the indexed view is to select from the view using the NOEXPAND hint. See msdn.microsoft.com/en-us/library/ms181151.aspx Jun 18, 2009 at 19:06
  • 1
    The only way on non-EE I mean Jun 18, 2009 at 19:07
0

One workaround is described here.

You create two views:

  1. an indexed view that has a set of rows for your first join and another set of rows for your second join, i.e. it's un-pivoted the data. This view can be indexed.
  2. a not-indexed view that takes your indexed view and PIVOTs it back to separate columns

In this workaround you do get an indexed view that does the joins, but SQL needs to do an extra PIVOT operation to get the data how you want to use it. Whether this will improve performance vs just doing the joins without an indexed view I don't know. (But if you do the analysis please let me know).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.