1

I am currently trying to optimize sql code. I would like to know if there is an alternate way of writing these statements, because it seems take a good deal of time to complete.

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

This does have over 22,000 entries.

4

2 回答 2

3

不能肯定这会更快,因为它取决于数据,但单个更新语句可能表现最好。

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-11T18:49:02.507 回答
1

我看到的第一部分是您可以结合这两个更新语句:

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 '%, %';

LEFT与匹配相比,执行的LIKE性能应该更高一些(注意:不确定,您必须对其进行测试才能验证)。

于 2012-06-11T18:32:42.673 回答