1.關於exists和in
in是循環的方式,在內存中處理,
exists是執行數據庫查詢,
select tpd.personaccountid,sum(nvl(tpd.CREDIT_SUM, 0)) as bjsr, sum(nvl(tpd.INTEREST_INCOME, 0)) as bjsr, sum(nvl(tpd.DEBIT_SUM, 0)) as bjzc from TGP_PERSON_DETAIL tpd
left join TAP_FUNDBUSINESS tfb
on tpd.FUNDBUSINESS_ID = tfb.FUNDBUSINESS_ID
where tpd.PERSONACCOUNTID
in (
select personaccountid from TGP_PERSONACCOUNT pa
left join tgp_person person
on pa.personid = person.personid
where person.zjhm = '330903196209160251'
) group by tpd.personaccountid
執行100次 耗時0.017S左右
select tpd.personaccountid,sum(nvl(tpd.CREDIT_SUM, 0)) as bjsr, sum(nvl(tpd.INTEREST_INCOME, 0)) as bjsr, sum(nvl(tpd.DEBIT_SUM, 0)) as bjzc from TGP_PERSON_DETAIL tpd
left join TAP_FUNDBUSINESS tfb
on tpd.FUNDBUSINESS_ID = tfb.FUNDBUSINESS_ID
where
exists (
select personaccountid from (select personaccountid from TGP_PERSONACCOUNT pa
left join tgp_person person
on pa.personid = person.personid
where person.zjhm = '330903196209160251') a
where tpd.personaccountid = a.personaccountid
) group by tpd.personaccountid
執行100次 耗時0.020S左右
以上都為SELECT FORM a IN b(OR EXISTS B) B中數據少的時候 采用IN 多的時候采用EXISTS
select tpd.personaccountid,sum(nvl(tpd.CREDIT_SUM, 0)) as bjsr, sum(nvl(tpd.INTEREST_INCOME, 0)) as bjsr, sum(nvl(tpd.DEBIT_SUM, 0)) as bjzc from TGP_PERSON_DETAIL tpd
left join TAP_FUNDBUSINESS tfb
on tpd.FUNDBUSINESS_ID = tfb.FUNDBUSINESS_ID
HAVING
exists (
select personaccountid from (select personaccountid from TGP_PERSONACCOUNT pa
left join tgp_person person
on pa.personid = person.personid
where person.zjhm = '330903196209160251') a
where tpd.personaccountid = a.personaccountid
) group by tpd.personaccountid
執行100次 耗時2.36S左右
因此先篩選數據再分組 效率更高
