Monday, 21 October 2013

Indexed Views

When  we create an Clustered Index on a view with schema binding then it is called Indexed View.
Indexed views are equivalent to Materialized View. i.e View that stores data.

Indexed Views are used for performance tuning.

Example:

CREATE VIEW vwProducts(ProductSubcategoryKey,ProductCategory,Product#)
with schemabinding
as
(
    SELECT
        p.ProductSubcategoryKey
        ,EnglishProductSubCategoryName
        ,COUNT_BIG(*)
    FROM[dbo].[DimProductSubcategory] psc  
    join [dbo].[DimProduct] p
        on p.ProductSubcategoryKey=psc.ProductSubcategoryKey
    group by EnglishProductSubCategoryName, p.ProductSubcategoryKey
)


SELECT * FROM vwProducts


CREATE UNIQUE CLUSTERED INDEX IDX_ProductSubcategoryKey
ON [dbo].[vwProducts](ProductSubcategoryKey)

SELECT * FROM vwProducts




No comments:

Post a Comment