有A和B两个表,A表有10W+数据,B表有100W+数据

有A和B两个表,A表有10W+数据,B表有100W+数据,要查询出在A表中但是不在B表中的数据。
A,B表的结构如下:
create table A(
`id` INT (255) not null,
primary key (`id`)
);

create table B(
`id` INT (255) not null,
primary key (`id`)
);
求教解决方法。注意数据量比较大
我一开始的回答是:使用not in进行集合运算,但是这样的话,select id from b要加载100W行数据到内存
select id from a
where id not in 
(select id from b)
全部评论
可以使用not exists
点赞
送花
回复
分享
发布于 2015-11-29 17:57
同问,遇到了类似问题
点赞
送花
回复
分享
发布于 2015-11-30 10:25
滴滴
校招火热招聘中
官网直投
使用A表和B表各五万条数据做了几次测试,使用not in和not exist的性能差别不大,甚至not in 还好些。 not exists的语句如下:  select * from a where not exists(  select * from b  where b.id = a.id ) 运行时间对比: not in: 0.161s    0.160s    0.160s     0.161s     0.161s    0.187s    0.169s    0.175s    0.161s    0.171s not ex:0.176s     0.176s     0.178s    0.201s    0.259s    0.184s    0.176s    0.190s    0.177s    0.176s 所以,个人感觉使用not exists不是解决方法
点赞
送花
回复
分享
发布于 2015-12-03 15:47

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务