Подзапросы в Python - PullRequest
       6

Подзапросы в Python

0 голосов
/ 08 октября 2019

Я пытаюсь использовать подзапросы, чтобы выполнить сопоставление с несколькими таблицами и переместить несопоставленные записи в новую таблицу.

Я написал подзапросы 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))

1 Ответ

1 голос
/ 08 октября 2019

Вы можете заменить как минимум четыре подзапроса одним:

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
)
...