我正在尝试提高某些查询的效率。由于我的知识有限,我一直在使用Right external join从多个表中引入完整的数据集,然后通过where语句将其剔除。但是,由于我想在提供我的架构绑定视图之一的跟随查询中添加“聚集索引”,我只是想知道是否有办法将以下“右外部连接”转换为“内部连接”,从而可以创建索引以减少加载时间。
在过去2-3天里,我一直在这个街区上狂奔,任何建议将不胜感激!
FROM dbo.Table1
RIGHT OUTER JOIN dbo.Table2
ON dbo.Table1.[Client ID] = dbo.Table2.[Client ID]
LEFT OUTER JOIN dbo.Table4
ON dbo.Table2.[Account Officer] = dbo.Table4.FullName
RIGHT OUTER JOIN dbo.Table3
ON dbo.Table2.[Account ID] = dbo.Table3.[Account ID]
WHERE (dbo.Table2.[Client Type] LIKE '%Customer 1%')
AND (dbo.Table3.ServiceDescription NOT LIKE '%Nil%')
AND (dbo.Table2.[Indicative outcome] IS NULL)
AND (dbo.Table2.[Finalised outcome] IS NULL)
AND (dbo.Table2.Outcome IS NULL)
AND (dbo.Table2.ReviewOutcome IS NULL)
AND (dbo.Table3.[Account Type] = 15)
AND (dbo.Table3.AccountReviewComplete = 1)
我试图重新安排它,以便某些条件进入联接,就像我在一些发现的文章中看到的那样(显然会限制开始时的数据大小),据说这会增加效率,但是在回顾了“实时查询统计信息”前后之后,似乎没有太大的区别。下面的示例:
FROM dbo.Table1
RIGHT OUTER JOIN dbo.Table2
ON dbo.Table1.[Client ID] = dbo.Table2.[Client ID]
LEFT OUTER JOIN dbo.Table4
ON dbo.Table2.[Account Officer] = dbo.Table4.FullName
RIGHT OUTER JOIN dbo.Table3
ON dbo.Table2.[Account ID] = dbo.Table3.[Account ID]
AND (dbo.Table3.[Account Type] = 15)
AND (dbo.Table3.AccountReviewComplete = 1)
WHERE (dbo.Table2.[Client Type] LIKE '%Customer 1%')
AND (dbo.Table3.ServiceDescription NOT LIKE '%Nil%')
AND (dbo.Table2.[Indicative outcome] IS NULL)
AND (dbo.Table2.[Finalised outcome] IS NULL)
AND (dbo.Table2.Outcome IS NULL)
AND (dbo.Table2.ReviewOutcome IS NULL)
答案 0 :(得分:0)
您没有提供足够的信息,但是我认为这可能会有所帮助。试试看:
SELECT *
FROM dbo.Table1 cd --ClientData
INNER JOIN dbo.Table2 jd --Junction Data
ON cd.[Client ID] = jd.[Client ID]
INNER JOIN dbo.Table3 ai --AccountInformation
ON jd.[Account ID] = ai.[Account ID]
LEFT JOIN dbo.Table4 ao --AccountOfficer
ON jd.[Account Officer] = ao.FullName
WHERE (jd.[Client Type] LIKE '%Customer 1%')
AND (ai.ServiceDescription NOT LIKE '%Nil%')
AND (jd.[Indicative outcome] IS NULL)
AND (jd.[Finalised outcome] IS NULL)
AND (jd.Outcome IS NULL)
AND (jd.ReviewOutcome IS NULL)
AND (ai.[Account Type] = 15)
AND (ai.AccountReviewComplete = 1)
一些提示:
inner join
将仅返回具有匹配链接行的行。LEFT JOIN
,因为其名称上的链接可能是错误的(typos!),否则您可能会错过任何行。LIKE
与前导%
一起使用将不允许索引,并使查询变慢。如果有机会,您应该将Client Type
和ServiceDescription
替换为确定集中的数据(如果可能,请使用目录表)