【问题标题】:Optimize TSQL Except优化 TSQL 除了
【发布时间】:2013-11-14 23:20:43
【问题描述】:

如果我可以包含表 MasterStaging(临时表)中的 ID,我可以优化连接:

  • EXCEPT 中包含ID 会扭曲结果,因为MasterStaging 中的ID 总是与StatusComparison 不同
  • MasterStaging.ID 无关紧要,只是一个自动编号,不代表客户 ID
  • SQL 旨在显示缺少的客户,无论 ID 是什么。
  • CustomerAccountNo 不是唯一编号

如何在JOIN包含获取ID top优化?

这就是我想要的:

与下面的 SQL 相同,但我将 JOIN 仅使用 ID

) x ON e.ID = x.ID  

这是我目前所拥有的:

UPDATE ecl.MasterStaging 
SET NewAccount = 1
    FROM ecl.MasterStaging e WITH (NOLOCK)
    JOIN (
          SELECT  
              ISNULL(Usable, 0) AS Usable ,
              ISNULL(TypeRC, 0) AS TypeRC ,
              ISNULL(CustomerNumber, 0) AS CustomerNumber ,
              ISNULL(CustomerAccountNo, 0) AS CustomerAccountNo ,
              ISNULL(LoadProfileClass, 0) AS LoadProfileClass ,
              ISNULL(MeterNo, 0) AS MeterNo ,
              ISNULL(PrimaryPhoneNumber, 0) AS PrimaryPhoneNumber ,
              ISNULL(CustomerName1, 0) AS CustomerName1 ,
              ISNULL(ServiceAddress1, 0) AS ServiceAddress1 ,
              ISNULL(ServiceCity, 0) AS ServiceCity ,
              ISNULL(ServiceState, 0) AS ServiceState ,
              ISNULL(ServiceZip, 0) AS ServiceZip ,
              ISNULL(BillingAddress1, 0) AS BillingAddress1 ,
              ISNULL(BillingCity, 0) AS BillingCity ,
              ISNULL(BillingState, 0) AS BillingState ,
              ISNULL(substring(BillingZip, 1, 5), 0) as BillingZip ,
              ISNULL(substring(BillingZip4, 7, 4), 0) as BillingZip4
          FROM    
              ecl.MasterStaging WITH (NOLOCK)           

          EXCEPT

           SELECT  Usable ,
            TypeRC ,
            CustomerNumber ,
            CustomerAccountNo ,
            LoadProfileClass ,
            MeterNo ,
            PrimaryPhoneNumber ,
            CustomerName1 ,
            ServiceAddress1 ,                   
            ServiceCity ,
            ServiceState ,
            ServiceZip ,
            BillingAddress1 ,                   
            BillingCity ,
            BillingState ,
            BillingZip ,
            BillingZip4 
    FROM    ecl.StatusComparison  WITH (NOLOCK)
    WHERE   [Status] <> 'D'
            ) x
    ON 
            ISNULL(e.Usable,0) = x.Usable AND 
            ISNULL(e.TypeRC,0) = x.TypeRC AND 
            ISNULL(e.CustomerNumber,0) = x.CustomerNumber AND 
            ISNULL(e.CustomerAccountNo,0) = x.CustomerAccountNo AND
            ISNULL(e.LoadProfileClass,0) = x.LoadProfileClass AND
            ISNULL(e.MeterNo,0) = x.MeterNo AND
            ISNULL(e.PrimaryPhoneNumber,0) = x.PrimaryPhoneNumber AND
            ISNULL(e.CustomerName1,0) = x.CustomerName1 AND
            ISNULL(e.ServiceAddress1,0) = x.ServiceAddress1 AND 
            ISNULL(e.ServiceCity,0) = x.ServiceCity AND
            ISNULL(e.ServiceState,0) = x.ServiceState AND
            ISNULL(e.ServiceZip,0) = x.ServiceZip AND
            ISNULL(e.BillingAddress1,0) = x.BillingAddress1 AND
            ISNULL(e.BillingCity,0) = x.BillingCity AND
            ISNULL(e.BillingState,0) = x.BillingState AND
            ISNULL(e.BillingZip,0) = x.BillingZip AND
            ISNULL(e.BillingZip4,0) = x.BillingZip4

【问题讨论】:

    标签: sql-server tsql


    【解决方案1】:

    一个想法,但这可能并不理想:您可以在两个表上创建一个计算和持久的列,该列将计算所有列的校验和,并且您可以加入它。不过有碰撞的风险:

    (我没有测试代码,如果有错误请告诉我):

    ALTER TABLE ecl.MasterStaging
    ADD hash AS CHECKSUM(Usable ,
                TypeRC ,
                CustomerNumber ,
                CustomerAccountNo ,
                LoadProfileClass ,
                MeterNo ,
                PrimaryPhoneNumber ,
                CustomerName1 ,
                ServiceAddress1 ,                   
                ServiceCity ,
                ServiceState ,
                ServiceZip ,
                BillingAddress1 ,                   
                BillingCity ,
                BillingState ,
                BillingZip ,
                BillingZip4 ) PERSISTED
    

    缺点:

    • 更多存储空间
    • 在插入和更新期间进行更多计算
    • 您需要更改表的结构
    • 如果您需要在 LOB 上进行比较,它将不起作用

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多