0

我正在尝试使用子查询对多个表运行匹配并将不匹配的记录移动到新表。

我已经编写了 SQL 子查询,但我面临的唯一问题是性能,它需要大量时间来处理。

create table UnmatchedRecord
(select a.* 
 from HashedValues a 
 where a.Address_Hash not in(select b.Address_Hash 
                             from HashAddress b) 
 and a.Person_Hash not in(select d.Person_Hash 
                          from HashPerson d) 
 and a.HH_Hash not in(select f.HH_Hash 
                      from HashHH f) 
 and a.VehicleRegistration not in(select VehicleRegistration 
                                  from MasterReference) 
 and a.EmailAddress not in (select EmailAddress 
                            from MasterReference) 
 and a.PhoneNumber not in (select PhoneNumber 
                           from MasterReference) 
 and a.NationalInsuranceNo not in (select NationalInsuranceNo 
                                   from MasterReference))
4

1 回答 1

1

您至少可以用一个替换四个子查询:

select HashedValues.*
from HashedValues
where not exists (
    select *
    from MasterReference
    where HashedValues.VehicleRegistration = MasterReference.VehicleRegistration
    or    HashedValues.EmailAddress        = MasterReference.EmailAddress
    or    HashedValues.PhoneNumber         = MasterReference.PhoneNumber
    or    HashedValues.NationalInsuranceNo = MasterReference.NationalInsuranceNo
)
and not exists (
    select *
    from HashAddress
    where HashedValues.Address_Hash = HashAddress.Address_Hash
)
and not exists (
    select *
    from HashPerson
    where HashedValues.Person_Hash = HashPerson.Person_Hash
)
and not exists (
    select *
    from HashHH
    where HashedValues.HH_Hash = HashHH.HH_Hash
)
于 2019-10-08T10:16:00.050 回答