我可以为所有索引需求创建视图吗?

时间:2017-03-17 02:41:08

标签: sql sql-server tsql sql-server-2012

我有3个系统:

Billing System
Tax System
AmericaGreatAgain System

...直接与我的数据库对话。

Employee table
columns: pk, ssn, last, first, dob
  1. 结算系统取决于 ssn 的索引
  2. Tax 系统取决于 last / first 索引
  3. AmericaGreatAgain 系统取决于 dob 的索引。
  4. 为了适应所有三个系统,它是否是在同一个Employee表上创建视图的标准解决方案,以满足其消费者的索引需求?

    在以下示例中,我们将创建一个视图并为该视图编制索引:

    CREATE VIEW Sales.vSalesSummaryCustomerProduct
    WITH SCHEMABINDING
    AS
        SELECT  CUST.CustomerID ,
                SOH.SalesOrderID ,
                SOD.ProductID ,
                SUM(SOD.OrderQty) AS TotalOrderQty ,
                SUM(LineTotal) AS TotalValue ,
                COUNT_BIG(*) AS CountLines
        FROM    Sales.SalesOrderHeader SOH
                INNER JOIN Sales.SalesOrderDetail SOD
                       ON SOH.SalesOrderID = SOD.SalesOrderID
                INNER JOIN Production.Product PROD
                       ON PROD.ProductID = SOD.ProductID
                INNER JOIN Sales.Customer CUST
                       ON SOH.CustomerID = CUST.CustomerID
                INNER JOIN Person.Person PER
                       ON PER.BusinessEntityID = CUST.PersonID
        GROUP BY CUST.CustomerID ,
                SOH.SalesOrderID ,
                SOD.ProductID; 
    GO
    CREATE UNIQUE CLUSTERED INDEX CX_vSalesSummaryCustomerProduct
      ON Sales.vSalesSummaryCustomerProduct(CustomerID, SalesOrderID, ProductID); 
    GO
    

    问题是,同一结果集需要一个单独的索引:

    CREATE VIEW Sales.vSalesSummaryCustomerProduct1 ---- new one
    WITH SCHEMABINDING
    AS
        SELECT  CUST.CustomerID ,
                SOH.SalesOrderID ,
    --etc
    index on another column
    
    
    CREATE VIEW Sales.vSalesSummaryCustomerProduct2 ---- new one
    WITH SCHEMABINDING
    AS
        SELECT  CUST.CustomerID ,
                SOH.SalesOrderID ,
    --etc
    index on another column
    

    通过简单地创建相同BASE数据的许多视图来创建这种类型的速度优化策略是什么标准做法,创建所需的索引?

0 个答案:

没有答案