表A结构
ID,手机号表B结构
ID,手机号1,手机号2,手机号3想查询表A中的手机,在表B的三个手机号均不存在的记录。
表A数据 11W条,表B数据15W条。在表A的手机号上增加了索引
在表B的三个手机上,分别加了索引,以及加了联合索引如下两种写法,执行时间都超级长,跑不出结果。
EXPLAIN
select count(*) 
from a
where not exists(
select * from b 
where a.手机号 = b.手机号1 or a.手机号 = b.手机号2or a.手机号 = b.手机号3 
)
执行计划:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a index idx_mobile 48 116090 Using index
1 SIMPLE b ALL 154113 Using where; Not existsEXPLAIN
select count(*) 
from family_contacts a left join datacenter_ods.crm_zdytable_52843_10893_52843_view b 
on a.手机号 = b.手机号1 or a.手机号 = b.手机号2 or a.手机号 = b.手机号3 
where b.id is null 
执行计划:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a index idx_mobile 48 116090 Using index
1 SIMPLE b ALL 154113 Using where; Not exists请指教。

解决方案 »

  1.   

    SELECT * FROM B AS b WHERE (SELECT COUNT(1) AS num FROM A AS a WHERE a.手机号 = b.手机号1 or a.手机号 = b.手机号2 or a.手机号 = b.手机号3 )=0;
    试试
      

  2.   

    select min(num1) from (
    select count(*) num1
    from a
    where not exists(
    select * from b 
    where a.手机号 = b.手机号1 
    )
    union all
    select count(*) num1
    from a
    where not exists(
    select * from b 
    where a.手机号 = b.手机号2 
    )
    union all
    select count(*) num1
    from a
    where not exists(
    select * from b 
    where a.手机号 = b.手机号3
    )
    )T
      

  3.   

    select id from A left join B on a.telnumber = b.telnumber and b.telnum is null
      

  4.   

    select *
    from a
    where not exists (select 1 from b where 手机号1=a.手机号)
    and  not exists (select 1 from b where 手机号2=a.手机号)
    and  not exists (select 1 from b where 手机号3=a.手机号)如果仍然有问题则
    以文本方式贴出(不要贴图!)
    explain select ....

    show index from 以供分析