sql效率问题

yuepengfei 2010-06-02 11:07:35
a表有数据15万,b表有数据30万

a表和b表通过id关联,现在要找到a表中的id在b表中不存在的。

如何写sql为高效?

试过not in,not exists left join 均未果。

请大侠给个办法,谢谢!
...全文
62 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2010-06-03
  • 打赏
  • 举报
回复
select a.* from a left join b on a.id=b.id where b.id is null

这种需求至少要存在对A表的全表扫描,无法避免。
yuepengfei 2010-06-03
  • 打赏
  • 举报
回复
索引是有的

explain之后的结果为

1;SIMPLE;a;ALL;NULL;NULL;NULL;NULL;155801;
1;SIMPLE;b;index;NULL;idx_a;63;NULL;283545;Using index;

显示a表是全表扫,b用上了索引,a表数据为155801,b表为283545

请问这是否是最佳的办法了?
ACMAIN_CHM 2010-06-02
  • 打赏
  • 举报
回复
a,b 表上ID是否有索引?

show index from a;
show index from b;

explain select ....
贴出来看一下。
WWWWA 2010-06-02
  • 打赏
  • 举报
回复
SHOW CREATE TABLE TBNAME
看看
yuepengfei 2010-06-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wwwwa 的回复:]
在A、B表ID字段上建立索引
select a.* from a left join b on a.id=b.id where b.id is null
[/Quote]


这个sql我写过的,不行的,速度依然很慢。
懒得去死 2010-06-02
  • 打赏
  • 举报
回复
..........:)...........
WWWWA 2010-06-02
  • 打赏
  • 举报
回复
在A、B表ID字段上建立索引
select a.* from a left join b on a.id=b.id where b.id is null

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧