表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请指教。
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请指教。
试试
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
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 以供分析