indexes in view

Shambhu Rai 1,406 Reputation points
2023-02-02T16:38:54.7266667+00:00

Hi Expert,

How to create indexes in view as it is taking more than hour to run

alter view view1 as select col1,col2,col3... from table1... total 35 columns

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,710 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,799 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,452 questions
{count} votes

Accepted answer
  1. CosmogHong-MSFT 22,941 Reputation points Microsoft Vendor
    2023-02-03T02:52:38.9533333+00:00

    Hi @Shambhu Rai

    Unlike regular views, indexed views are materialized views that stores data physically like a table hence may provide some the performance benefit if they are used appropriately.

    To create an indexed view, you use the following steps:

    Because of the WITH SCHEMABINDING option, if you want to change the structure of the underlying tables which affect the indexed view’s definition, you must drop the indexed view first before applying the changes.

    In addition, SQL Server requires all object references in an indexed view to include the two-part naming
    convention i.e., schema.object, and all referenced objects are in the same database.

    Refer to this article for more details and samples: SQL Server Indexed View

    --add a unique clustered index to the view:
    CREATE UNIQUE CLUSTERED INDEX 
           ucidx_product_id 
    ON production.product_master(product_id);
    
    --add a non-clustered index to the view:
    CREATE NONCLUSTERED INDEX 
        ucidx_product_name
    ON production.product_master(product_name);
    

    Note: When the data of the underlying tables changes, the data in the indexed view is also automatically updated. This causes a write overhead for the referenced tables. It means that when you write to the underlying table, SQL Server also has to write to the index of the view. Therefore, you should only create an indexed view against the tables that have in-frequent data updates.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 101K Reputation points MVP
    2023-02-02T22:40:31.9233333+00:00

    It is unlikely that an indexed view is the solution to your problem.

    Since you did not show the query you are actually running or the full view definition, we cannot say anything more.

    2 people found this answer helpful.

  2. Alberto Morillo 32,886 Reputation points MVP
    2023-02-02T17:14:41.9733333+00:00

    You can create indexed views and you can read here the basics of indexed views to improve performance. Take in consideration this indexed views do not support all data types and not all the features. Read more pros and cons about indexed views here, before you use them.

    Please try not to create nested views or you will find query plans will get more complicated.

    One personal suggestion, sometimes when you want to build a view, you should really build a stored procedure, especially on reporting systems scenarios. Stored procedures allow parameters and can receive a reduced result data set and perform faster.

    1 person found this answer helpful.
    0 comments No comments

  3. Tom Cooper 8,466 Reputation points
    2023-02-02T17:22:17.6066667+00:00

    You can create indexes on views. But indexed views have a lot of restrictions and may cause performance problems, particularly if there are lots of changes to the data in the underlying table(s).

    The documentation for creating and using indexed views is available at https://learn.microsoft.com/en-us/sql/relational-databases/views/create-indexed-views?view=sql-server-ver16

    Tom

    0 comments No comments

  4. Shambhu Rai 1,406 Reputation points
    2023-02-02T20:53:41.89+00:00

    any index example please

    0 comments No comments