我目前正在尝试优化sql代码。我想知道是否有其他方法来编写这些语句,因为它似乎需要很长时间才能完成。
Update #TMP---updates webid when its null in tmp table
Set #TMP.webid_Val='NOT COMPLIANT'
Where #TMP.webid is null
Update #TMP---updates PID when its null in tmp table
Set #TMP.PID_Val='NOT COMPLIANT'
Where #TMP.Pid is null
Update #TMP---Shifts multiple fob situations into storewide
Set #TMP.GMM ='Storewide'
Where #TMP.gmm like '%, %';
Update #TMP-----Shifts marketing into multiple fob situation
Set #TMP.GMM ='Storewide'
Where #TMP.gmm like 'Marketing%'
Update #TMP
Set #TMP.OVERALL_Val='NOT COMPLIANT'
Where #TMP.webid is null这确实有超过22,000个条目。
发布于 2012-06-12 02:49:03
不能肯定这会更快,因为它取决于数据,但单个update语句可能执行得最好。
Update #TMP
Set #TMP.webid_Val=
CASE
WHEN #TMP.webid is null THEN 'NOT COMPLIANT'
ELSE #TMP.webid_Val
END
,#TMP.PID_Val=
CASE
WHEN #TMP.Pid is null THEN 'NOT COMPLIANT'
ELSE #TMP.PID_Val
END
,#TMP.GMM=
CASE
WHEN (#TMP.GMM like '%, %' OR #TMP.gmm like 'Marketing%') THEN 'Storewide'
ELSE #TMP.GMM
END
,#TMP.OVERALL_Val=
CASE
WHEN (#TMP.webid is null) THEN 'NOT COMPLIANT'
ELSE #TMP.OVERALL_Val
END
WHERE #TMP.webid is null
OR #TMP.Pid is null
OR #TMP.gmm like '%, %'
OR #TMP.gmm like 'Marketing%'发布于 2012-06-12 02:32:43
我看到的第一个部分是,您可以组合这两个update语句:
Update #TMP---updates webid when its null in tmp table
Set #TMP.webid_Val='NOT COMPLIANT'
Where #TMP.webid is null
Update #TMP
Set #TMP.OVERALL_Val='NOT COMPLIANT'
Where #TMP.webid is null进入:
Update #TMP---updates webid when its null in tmp table
Set #TMP.webid_Val='NOT COMPLIANT',
#TMP.OVERALL_Val='NOT COMPLIANT'
Where #TMP.webid is null您可以将这两个GMM更新合并为以下内容:
Update #TMP---Shifts multiple fob situations into storewide
Set #TMP.GMM ='Storewide'
Where LEFT(#TMP.gmm, 9) = 'Marketing'
OR #TMP.gmm like '%, %';与LIKE匹配相比,执行LEFT应该会有更高的性能(注意:不能确定这一点,您必须对其进行测试以进行验证)。
https://stackoverflow.com/questions/10985507
复制相似问题