我有一个查询,可以在SQL Server中保持超时。查询中最大的表只有几百行。基本上我正在尝试将tblNovaPosts中的novaPost字段与一堆表中的数据进行匹配,以创建报告。我该如何更改此查询以使其运行得更快?
SELECT TOP (100) PERCENT tblNovaPosts.type, tblNovaPosts.novaPost,
ISNULL(SUM(tblAuditTrail.amount), 0) AS FUNDING,
ISNULL(SUM(tbl510.allot), 0) AS ALLOT,
ISNULL(SUM(tblStanfin.oblig), 0)
+ ISNULL(SUM(tblSof.obligationsCum), 0)
+ ISNULL(SUM(tblSpecAppt.obligations), 0)
+ ISNULL(SUM(tblJlens.obligationsCum), 0)
- ISNULL(SUM(viewReimbObs.reimbObs), 0) AS OBLIGATED,
ISNULL(SUM(tblSof.commitmentsNonCum), 0)
+ ISNULL(SUM(tblRmt.commitment),0)
+ ISNULL(SUM(tblReimb.commitmentsNonCum), 0)
- ISNULL(SUM(viewReimbObs.reimbObs), 0) AS DIRCOMMIT
FROM tblNovaPosts LEFT OUTER JOIN
tblAuditTrail ON tblNovaPosts.novaPost = tblAuditTrail.novaPost LEFT OUTER JOIN
tbl510 ON tblNovaPosts.novaPost = tbl510.novaPost LEFT OUTER JOIN
tblStanfin ON tblNovaPosts.novaPost = tblStanfin.novaPost LEFT OUTER JOIN
tblSof ON tblNovaPosts.novaPost = tblSof.novaPost LEFT OUTER JOIN
tblSpecAppt ON tblNovaPosts.novaPost = tblSpecAppt.novaPost LEFT OUTER JOIN
tblJlens ON tblNovaPosts.novaPost = tblJlens.novaPost LEFT OUTER JOIN
viewReimbObs ON tblNovaPosts.novaPost = viewReimbObs.novaPost1 LEFT OUTER JOIN
tblRmt on tblNovaPosts.novaPost = tblRmt.novaPost LEFT OUTER JOIN
tblReimb ON tblNovaPosts.novaPost = tblReimb.novaPost
GROUP BY tblNovaPosts.type, tblNovaPosts.novaPost
ORDER BY tblNovaPosts.type, tblNovaPosts.novaPost
答案 0 :(得分:1)
查看查询计划。通常,SQL Server会为您提供索引建议。
点击此按钮:
答案 1 :(得分:0)
所有LEFT OUTER JOIN都有可疑之处。你确定这是你需要的吗?
另外,我注意到你正在加入一个名为tblAuditTrail的表。这似乎可能产生2个表的笛卡尔积。
也许您应该单独测试每个连接,以检查您实际上并没有请求十亿行而不是您期望的几百行。