jerry xu
jerry xu

Reputation: 1

Is it recommended to use indexed view on static columns in a frequent changing table?

I am learning indexed views in SQL Server, and I know some general limitation (like we can't do cross database joins etc) and in general, the benefit may not be that significant when the underlying table is changed frequently.

In my scenario, I have a table, say Orders which contains around 1 million rows. The schema is like this:

CREATE TABLE dbo.Orders 
(
    OrderID INT PRIMARY KEY,
    OrderCitiCode varchar(15),
    SpecialOrderType varchar(15),
    PrimaryOrderCitiCode varchar(15),
    xxxxx  --other columns that may get updated frequently
);

The 4 columns listed are not updated frequently, but other columns in this table may get changed fairly often.

I have requirements to find the OrderId of some order's SpecialOrderType, the query is like this (assume indexes are available on the columns I want to join)

SELECT
    o.Orderciticode, o.orderId,   
    SpecialOrderType = specialUnitOrders.SpecialUnitType_Id,
    SpecialOrderId = specialUnitOrders.id,
    SpecialOrderCitiCode = specialUnitOrders.CitiCode 
FROM
    orders with(nolock)  o 
INNER JOIN
    orders specialUnitOrders with(nolock) ON specialUnitOrders  .PrimaryOrderCiticode = orders.CitiCode 
WHERE
    SpecialOrderType = @SomeSpecialOrderType

We can see this join only use the more static columns and in theory, I can create an indexed view such as:

CREATE VIEW dbo.SpecialOrderTypes 
WITH SCHEMABINDING 
AS
    SELECT
        o.Orderciticode, o.orderId,    
        SpecialOrderType = specialUnitOrders.SpecialUnitType_Id, 
        SpecialOrderId = specialUnitOrders.id, 
        SpecialOrderCitiCode = specialUnitOrders.CitiCode    
    FROM
        orders o        
    INNER JOIN
        orders specialUnitOrders ON specialUnitOrders  .PrimaryOrderCiticode = orders.CitiCode 

Then query

SELECT * 
FROM SpecialOrderTypes 
WHERE SpecialOrderType = @SomeSpecialOrderType

Is it ok to create and use indexed view on a frequent updating table if we use the fields that in most cases will be static (no change)?

The view will be used in a quite busy system that get accessed millions of time every day.

Many thanks

Upvotes: 0

Views: 71

Answers (0)

Related Questions