2008-09-24 118 views
-1

我想指數這種看法,因爲它有子查詢我不能索引。任何人都可以建議如何改變這個觀點,以便我可以索引它。我該如何改變這種觀點?

ALTER VIEW [dbo].[Recon2] 
WITH SCHEMABINDING 
AS 
SELECT  
dbo.Transactions.CustomerCode, 
dbo.Customer_Master.CustomerName, 
dbo.Transactions.TransDate, 
dbo.Transactions.PubCode, 
dbo.Transactions.TransType, 
dbo.Transactions.Copies, 
SUM(dbo.Transactions.TotalAmount) AS TotalAmount, 
'0' AS ReceiptNo, 
'2008-01-01' AS PaymentDate, 
0 AS Amount, 
dbo.Transactions.Period, 
dbo.Transactions.Year, 
dbo.Publication_Master.PubName, 
dbo.Customer_Master.SalesCode, 
COUNT_BIG(*) AS COUNT 
FROM   
dbo.Publication_Master INNER JOIN 
dbo.Customer_Master INNER JOIN 
dbo.Transactions ON dbo.Customer_Master.CustomerCode = dbo.Transactions.CustomerCode ON 
dbo.Publication_Master.PubCode = dbo.Transactions.PubCode 
WHERE  
(dbo.Customer_Master.CustomerCode NOT IN 
(SELECT  CustomerCode 
FROM  dbo.StreetSaleRcpt 
WHERE  (PubCode = dbo.Transactions.PubCode) AND 
      (TransactionDate = dbo.Transactions.TransDate) AND 
      (Updated = 1) AND 
      (PeriodMonth = dbo.Transactions.Period) AND 
      (PeriodYear = dbo.Transactions.Year))) 
GROUP BY dbo.Transactions.CustomerCode, dbo.Customer_Master.CustomerName, dbo.Transactions.TransDate, dbo.Transactions.PubCode, 
         dbo.Publication_Master.PubName, dbo.Customer_Master.SalesCode, dbo.Transactions.[Update], dbo.Transactions.TransType, 
         dbo.Transactions.Copies, dbo.Transactions.Period, dbo.Transactions.Year, dbo.Transactions.TotalAmount 

回答

3

我無法運行(顯然),但你看這個?:

SELECT 
dbo.Transactions.CustomerCode, 
dbo.Customer_Master.CustomerName, 
dbo.Transactions.TransDate, 
dbo.Transactions.PubCode, 
dbo.Transactions.TransType, 
dbo.Transactions.Copies, 
'0' AS ReceiptNo, 
'2008-01-01' AS PaymentDate, 
0 AS Amount, 
dbo.Transactions.Period, 
dbo.Transactions.Year, 
dbo.Publication_Master.PubName, 
dbo.Customer_Master.SalesCode, 
dbo.StreetSaleRcpt.CustomerCode, 
SUM(dbo.Transactions.TotalAmount) AS TotalAmount, 
COUNT_BIG(*) AS COUNT 
FROM dbo.Publication_Master 
INNER JOIN dbo.Customer_Master ON dbo.Customer_Master.CustomerCode = dbo.Transactions.CustomerCode 
INNER JOIN dbo.Transactions ON dbo.Publication_Master.PubCode = dbo.Transactions.PubCode 
LEFT OUTER JOIN dbo.StreetSaleRcpt ON (
    dbo.StreetSaleRcpt.PubCode = dbo.Transactions.PubCode 
    AND dbo.StreetSaleRcpt.TransactionDate = dbo.Transactions.TransDate 
    AND dbo.StreetSaleRcpt.PeriodMonth = dbo.Transactions.Period 
    AND dbo.StreetSaleRcpt.PeriodYear = dbo.Transactions.Year 
    AND dbo.StreetSaleRcpt.Updated = 1 
    AND dbo.StreetSaleRcpt.CustomerCode = dbo.Customer_Master.CustomerCode 
) 
WHERE dbo.StreetSaleRcpt.CustomerCode IS NULL 
GROUP BY 
dbo.Transactions.CustomerCode, 
dbo.Customer_Master.CustomerName, 
dbo.Transactions.TransDate, 
dbo.Transactions.PubCode, 
dbo.Publication_Master.PubName, 
dbo.Customer_Master.SalesCode, 
dbo.Transactions.[Update], 
dbo.Transactions.TransType, 
dbo.Transactions.Copies, 
dbo.Transactions.Period, 
dbo.Transactions.Year, 
dbo.Transactions.TotalAmount, 
dbo.StreetSaleRcpt.CustomerCode 

讓您的相關子查詢左連接和測試它的缺失('WHERE dbo.StreetSaleRcpt。 CustomerCode IS NULL')與'NOT IN'。

祝你好運。

0

至少在Oracle中,你可以從視圖更改爲實體化視圖。還有其他一些問題,比如表空間和同步方法需要考慮,但這可能值得探討。

根據您的應用,另一種選擇是創建基於選擇這一觀點的普通表,要麼在可接受的時間間隔更新或使用了大量的外鍵。

什麼是最實用取決於許多因素 - 表的大小,頻率的更新,需要最新數據,等等

+0

我使用SQL 2005和IAM設計爲每月核對報告視圖。這僅僅是一個查詢的一部分和IAM有工會加入其他parts..it返回1MB。 – jbcedge 2008-09-24 22:28:17

0

這種形式將允許使用索引的StreetSaleRcpt每個Publication_Master行:

ALTER VIEW [dbo].[Recon2] WITH SCHEMABINDING AS SELECT 
dbo.Transactions.CustomerCode, dbo.Customer_Master.CustomerName, dbo.Transactions.TransDate, dbo.Transactions.PubCode, dbo.Transactions.TransType, dbo.Transactions.Copies, SUM(dbo.Transactions.TotalAmount) AS TotalAmount, '0' AS ReceiptNo, '2008-01-01' AS PaymentDate, 0 AS Amount, dbo.Transactions.Period, dbo.Transactions.Year, dbo.Publication_Master.PubName, dbo.Customer_Master.SalesCode, COUNT_BIG(*) AS COUNT 
FROM dbo.Publication_Master 
INNER JOIN dbo.Customer_Master 
INNER JOIN dbo.Transactions ON dbo.Customer_Master.CustomerCode = dbo.Transactions.CustomerCode ON dbo.Publication_Master.PubCode = dbo.Transactions.PubCode 
WHERE 
(NOT EXISTS 
    (SELECT NULL FROM dbo.StreetSaleRcpt 
    WHERE (PubCode = dbo.Transactions.PubCode) 
    AND (TransactionDate = dbo.Transactions.TransDate) 
    AND (Updated = 1) 
    AND (PeriodMonth = dbo.Transactions.Period) 
    AND (PeriodYear = dbo.Transactions.Year) 
    ANMD (CustomerCode = dbo.Customer_Master.CustomerCode) 
    ) 
) GROUP BY dbo.Transactions.CustomerCode, dbo.Customer_Master.CustomerName, dbo.Transactions.TransDate, dbo.Transactions.PubCode, dbo.Publication_Master.PubName, dbo.Customer_Master.SalesCode, dbo.Transactions.[Update], dbo.Transactions.TransType, dbo.Transactions.Copies, dbo.Transactions.Period, dbo.Transactions.Year, dbo.Transactions.TotalAmount