我怎么能改变这个观点?

时间:2008-09-24 21:50:43

标签: sql views

我想索引此视图,但因为它有子查询我不能索引。任何人都可以建议如何更改此视图,以便我可以索引它。

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 个答案:

答案 0 :(得分: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'。

祝你好运。

答案 1 :(得分:0)

至少在Oracle中,您可以从VIEW更改为MATERIALIZED VIEW。还会有一些其他问题,比如表空间和同步方法,但可能值得探讨。

根据您的应用程序,另一个选项是根据此视图的选择创建一个普通表,并以可接受的间隔更新它或使用大量外键。

最实际的取决于许多因素 - 表格大小,更新频率,对大多数当前数据的需求等。

答案 2 :(得分: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