我遇到了一个不幸的情况:在开发环境中使用 SQL Server 2005,但在生产服务器上使用 SQL Server 2000 来开发应用程序。我有一个相当复杂的 SELECT 查询,它在开发/测试服务器上运行良好,但在生产环境中失败:
SELECT tbl_questions.Question, tbl_questions.QuestionCode
FROM tbl_questions INNER JOIN (
SELECT sg.questioncode, sg.gradeB, sg.gradeA, t2.wt
FROM tbl_scoregrade AS sg INNER JOIN (
SELECT t1.QuestionCode, AVG(1.0 * aw.Weight) AS wt
FROM tbl_AnswerWeight AS aw INNER JOIN (
SELECT assa.QuestionCode, assa.Answer
FROM tbl_AllStaffSurveyAnswers AS assa INNER JOIN
tbl_AllStaffSurvey AS ass ON assa.Questionguid = ass.Questionguid
WHERE (ass.Trust = 'RD7') AND (ass.Specialty = '97'))
AS t1 ON aw.questioncode = t1.QuestionCode AND aw.Response = t1.Answer
GROUP BY t1.QuestionCode )
AS t2 ON sg.questioncode = t2.QuestionCode AND sg.gradeA > t2.wt)
AS t3 ON tbl_questions.QuestionCode = t3.questioncode
您是否能看到在不同版本的 SQL Server 上运行时会产生影响的任何内容,或者在任何情况下是否有任何简化查询的方法?
最佳答案
这不是您问题的答案,而是需要考虑的重要事情。您有一个相当复杂的查询,它将消耗大量资源。由于复杂性,SQL Server 2000 中的优化器将无法创建优化的查询计划,并且可能会使用表扫描。在 SQL Server 2000 中不建议使用超过 4 个联接。
建议你尝试分割语句,使用临时表
要考虑的另一件事是使用“三角形连接”
ON sg.questioncode = t2.QuestionCode AND sg.gradeA > t2.wt
这是一篇关于 Triangular joins 的有趣文章
问候
哈坎温瑟
关于sql - SQL 2000 中的多个内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1333656/