求差集,在A表不在B表中的数据(两个表的差集),除了not in

2019-11-21  本文已影响0人  燃灯道童
from写错了,请忽略

经常遇到这种情况,平常写法也是如图,想思考下还有没有其他的写法?经过思考和集思广益后,有以下几种方法。

背景:

select count(0) from tps_uw_detail;##共61394条数据  A表

select  count(0) from  ld_intercept_file;##共19686条数据 B表

方法1:

SELECT a.app_no

FROM tps_uw_detail a

WHERE NOT EXISTS ( SELECT b.insur_no FROM ld_intercept_file b WHERE b.insur_no = a.app_no );

###耗时283.034s 查询出的保单数据51629

方法2:

SELECT a.app_no

FROM tps_uw_detail a

LEFT JOIN ld_intercept_file b ON b.insur_no = a.app_no

WHERE b.insur_no IS NULL;

###耗时122.584s 查询出的保单数据51629

SELECT a.app_no

FROM tps_uw_detail a

LEFT JOIN ld_intercept_file b ON b.insur_no = a.app_no

WHERE ( b.insur_no IS NULL OR b.insur_no = '' );

##耗时168.719s 查询出的保单数据51629

大致看了一下,还是用关联的速率会快一点。

上一篇 下一篇

猜你喜欢

热点阅读