Issue while creating indexed view after joining two non related tables

  • I am trying an create the indexed view after joining two non related tables. Please correct me if my approach is wrong and add your thoughts for the issue I had mentioned below.

    Syntax used for creating view

    CREATE VIEW MyViewName

    WITH SCHEMABINDING

    AS

    select field1, field 2 from tableOne (not the exact query)

    FULL OUTER JOIN

    select field1, field2 from tableTwo (not the exact query)

    Syntax used for creating index on view

    CREATE UNIQUE CLUSTERED INDEX ClusteredIndexTest

    ON dbo.MyViewName(field1,field2)

    Issue occurred while trying to create index

    Msg 10109

    Cannot create index on view "db.dbo.MyViewName" because it references derived table "tableOne" (defined by SELECT statement in FROM clause). Consider removing the reference to the derived table or not indexing the view.

  • Indexed views have some stringent requirements on what kind of view definition they can be created on. Derived tables are not allowed.

    Have a look here for a complete list of what's not allowed:

    http://msdn.microsoft.com/en-us/library/ms191432.aspx#Restrictions

  • Hi,

    OUTER JOINs are not supported in Indexed views because rows can logically disappear under some update operations in the base tables.

    Regards

    IgorMi

    Igor Micev,My blog: www.igormicev.com

  • Thanks for the reply.

    Is there any way to tackle my scenario mentioned here.?

    I need to combine (UNION) two non related derived tables and create an indexed view on top of that. Since UNION operation is not allowed under indexed views, I had tried to join then using OUTER JOIN. (I need data from both the non related derived tables unconditionally)

    Please advice.

  • shyam00 (7/5/2013)


    Thanks for the reply.

    Is there any way to tackle my scenario mentioned here.?

    I need to combine (UNION) two non related derived tables and create an indexed view on top of that. Since UNION operation is not allowed under indexed views, I had tried to join then using OUTER JOIN. (I need data from both the non related derived tables unconditionally)

    Please advice.

    You're experiencing difficulty in creating an indexed view. Ok, so backtrack for a moment. View indexing is a performance measure. Somewhere, you're attempting to use these two tables, JOINed or UNIONed or whatever, in another query. Can you show an example of such a query, and if possible an actual execution plan? In the event that the indexed view you would ideally like to see is impossible to create, there may well be alternative means of accelerating the queries that the view would by used by.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • shyam00 (7/5/2013)


    Thanks for the reply.

    Is there any way to tackle my scenario mentioned here.?

    I need to combine (UNION) two non related derived tables and create an indexed view on top of that. Since UNION operation is not allowed under indexed views, I had tried to join then using OUTER JOIN. (I need data from both the non related derived tables unconditionally)

    Please advice.

    Hi,

    Having an indexed view is almost same as having a table. As you have troubles creating it, why don't you think for another alternative?

    - You can investigate on improving the performance of the queries using those selects you want to put in a view.

    - You can create a separate table and add some work around to fill it appropriately.

    Regards,

    IgorMi

    Igor Micev,My blog: www.igormicev.com

  • shyam00 (7/5/2013)


    Thanks for the reply.

    Is there any way to tackle my scenario mentioned here.?

    I need to combine (UNION) two non related derived tables and create an indexed view on top of that. Since UNION operation is not allowed under indexed views, I had tried to join then using OUTER JOIN. (I need data from both the non related derived tables unconditionally)

    Please advice.

    And the advice is: start from the beginning. That is, why do you need this view? Why do you need it to be indexed?

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Currently I am building a search(filter) dash board application which is querying the underlying tables (OLTP) in MS SQL server 2008.

    There are around 10 tables I need to query for getting the consolidated search results based on the filter condition. Most of the tables can be joined together, but there are some un-related tables as well.

    There is performance trade off if I query each of these table and join them directly since number of concurrent users using this application will be more than 1000. Also some of the tables contain more than million records. That's why I didn't tried to use a stored procedure.

    Based on this I though of using an Indexed view which contain all the fields required by search filter. And this indexed view getting refreshed using some trigger. Is there any other approach I can use in this scenario?

  • Since the description you give is on a fairly high-level, it is difficult to give exact advice, but, no, from what you say I don't think indexed views is what you need.

    To say more, we would need to see the tables (or preferrably some simplified layout of them) and how they are related. It sounds a little funny that you would search 10 entirely unrelated tables, so I assume that there is some form of relation between them.

    What I can say is that indexing common search terms helps a log.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Out of those ten tables 9 are related. Only one table is non related one. I will give a sample layout soon.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply