合并查询需要很长时间

时间:2013-11-11 12:21:25

标签: sql sql-server

MERGE dimS AS T
USING ccservertbl AS S 
        ON (T.iAID = S.ServerID) OR (T.iAID = S.SID) 
        WHEN NOT MATCHED BY TARGET  
          THEN INSERT (
                iAID,
                ConfigItemName,
                PropertyName,
                NetWorkDomainName,
                DataCenterTLA,
                RetireValueIND,
                AssetOnlyInd,
                LastUpdatedDtim,
                ODataServerID) 
           VALUES (
                S.SID, 
                S.Name, 
                S.PropertyDimension, 
                S.NetworkDomain,
                S.datacenterCode, 
                S.RetiredvalueInd, 
                S.IsAssetonly, 
                S.Modified,
                S.ID)
        WHEN MATCHED 
         THEN UPDATE 
            SET T.ConfigItemName = S.Name,
                T.PropertyName = S.PropertyDimension, 
                T.NetWorkDomainName = S.NetworkDomain,
                T.DataCenterTLA = S.datacenterCode, 
                T.RetireValueIND = S.RetiredvalueInd, 
                T.AssetOnlyInd = S.IsAssetonly, 
                T.LastUpdatedDtim = S.Modified,
                T.ODataServerID = S.ID;

1 个答案:

答案 0 :(得分:0)

尽量不要访问整个对象dimSccservertbl

例如替换

MERGE dimS AS T
USING ccservertbl AS S

;WITH TargetTable AS (
    SELECT *
    FROM dimS
    WHERE <some slicing clause>
), SourceTable AS (
    SELECT *
    FROM ccservertbl
    WHERE <some slicing clause>
)
MERGE TargetTable AS T
USING SourceTable AS S