重写查询以避免使用自我联接

时间:2019-04-15 19:41:20

标签: sql-server-2012

我有一个使用自我联接编写的查询。该表有约360万行,需要几个小时才能运行。

update m
    SET
        [ChildGlobalFamilyUnique]  = [ParentGlobalFamilyUnique],
        [ChildGlobalFamilyDupID] = [ParentGlobalFamilyDupId],
        [ChildCustodianFamilyUnique] = [ParentCustodianFamilyUnique],
        ChildCustodainFamilyDupId = ParentCustodianFamilyDupID
    from(
        select
            child.DupGroupIdentifier
            child.NuixGuid [ChildGuid],
            child.toplvlguid [ChildTopLevelGuid],
            parent.[IsGlobalFamilyUnique] [ParentGlobalFamilyUnique],
            child.IsGlobalFamilyUnique [ChildGlobalFamilyUnique] ,
            parent.GlobalFamilyDupID [ParentGlobalFamilyDupId],
            child.GlobalFamilyDupID [ChildGlobalFamilyDupID],
            child.CustodianFamilyDupID [ChildCustodainFamilyDupId],
            child.IsCustodianFamilyUnique [ChildCustodianFamilyUnique],
            parent.CustodianFamilyDupID [ParentCustodianFamilyDupID],
            parent.IsCustodianFamilyUnique [ParentCustodianFamilyUnique]
        from [EDDS1029171_master].dbo.tblMaster parent
        inner join [EDDS1029171_master].dbo.tblMaster child
         on child.DupGroupIdentifier = parent.DupeIdentifier
                where child.IsCustodianFamilyUnique is null AND child.IsglobalFamilyUnique is null and child.IsChild = 1
                                AND parent.IsglobalFamilyUnique is not null AND parent.IsCustodianFamilyUnique is not null and parent.ischild = 0

    )m

我在DupGroupIdentifierDupeIdentifier上有一个非聚集索引。

如何重新编写以上代码,从而避免使用自连接并最终使查询停止运行数小时?

0 个答案:

没有答案