sql语句引起的效率问题

haigou20 2012-11-27 10:01:41
有同事说我的sql语句存在效率问题

我原来的语句是:
select a_id form t_a where a_id in (select o_id from t_o where t=3 and s=10)

同事说a_id虽然是主键也是索引,但是并未起到索引作用

经过优化后的语句:
select a_id form t_a where a_id in (select o_id from (select o_id from t_o where t=3 and s=10) t)

各位的看法如何?
...全文
120 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rucypli 2012-11-28
  • 打赏
  • 举报
回复
mysql中应该严禁出现in(子查询)的情况 !! select a_id form t_a where a_id in (select o_id from t_o where t=3 and s=10) 改成 select a_id from t_a A,t_o B where A.a_id=B.o_id and B.t=3 and B.s=10;
Rotel-刘志东 2012-11-28
  • 打赏
  • 举报
回复
分析一下不就知道了,explain执行计划不就知道了。
ACMAIN_CHM 2012-11-28
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/310109377 [求证&散分]MySQL 中 where id in (1,2,3,4,...) 的效率问题讨论
Rotel-刘志东 2012-11-28
  • 打赏
  • 举报
回复
引用 3 楼 rucypli 的回复:
mysql中应该严禁出现in(子查询)的情况 !! select a_id form t_a where a_id in (select o_id from t_o where t=3 and s=10) 改成 select a_id from t_a A,t_o B where A.a_id=B.o_id and B.t=3 and B.s=10;
explain试一下此方法应该效率比in高。
ACMAIN_CHM 2012-11-27
  • 打赏
  • 举报
回复
explain select ... 看一下执行计划,对比分析一下就知道了。

56,687

社区成员

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

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