我怎样才能改变这种看法?
Posted
技术标签:
【中文标题】我怎样才能改变这种看法?【英文标题】:how can i change this view? 【发布时间】:2008-09-24 21:50:43 【问题描述】:我想索引这个视图但是因为它有子查询我不能索引。谁能建议如何更改此视图以便我可以对其进行索引。
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
【问题讨论】:
【参考方案1】:我无法运行它(显然)但是这个呢?:
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'。
祝你好运。
【讨论】:
【参考方案2】:至少在 Oracle 中,您可以从 VIEW 更改为 MATERIALIZED VIEW。还有一些其他问题需要考虑,例如表空间和同步方法,但可能值得探索。
根据您的应用程序,另一种选择是基于此视图的选择创建一个普通表,并以可接受的间隔更新它或使用大量外键。
什么最实用取决于许多因素——表大小、更新频率、对最新数据的需求等。
【讨论】:
我正在使用 sql 2005 并正在为每月对帐报告设计一个视图。这只是查询的一部分,我有工会加入其他部分..它返回 1MB。【参考方案3】:此表单允许在 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
【讨论】:
以上是关于我怎样才能改变这种看法?的主要内容,如果未能解决你的问题,请参考以下文章
我想改变visualViewport的比例。我怎样才能务实地实现这一目标? [复制]
UI Sortable - 我怎样才能让一些 div 在一个 sortable 中保持完整并且在其他 sortables 改变时永远不会移动或改变
有来自 iMDB api 的图像链接,我怎样才能改变它的大小 pyton