mysql子查询效率_MySQL-mysql 子查询和多表查询速度 谁更快?

试了一下,没看出什么区别,有索引都会走索引,没有都全表扫描。

另外 第二个sql,left join是没有意义的,因为WHERE里判断t2.id=?,如果t2字段为null,这个条件肯定为unknown。

select * from t1 where uid=(select uid from t2 where uid=200009);

优化为:

mysql> show warnings;

-------------------------------------+

| Note | 1003 | select `test1`.`t1`.`id` AS `id`,`test1`.`t1`.`uid` AS `uid`,`test1`.`t1`.`type` AS `type` from `test1`.`t1` where (`test1`.`t1`.`uid` = (select `test1`.`t2`.`uid` from `test1`.`t2` where (`test1`.`t2`.`uid` = 200009)))

-------------------------------------+

1 row in set (0.00 sec)

select * from t1 left join t2 on t1.uid=t2.uid where t2.uid=200009;

优化为:

mysql> show warnings;

-----------------+

| Note | 1003 | select `test1`.`t1`.`id` AS `id`,`test1`.`t1`.`uid` AS `uid`,`test1`.`t1`.`type` AS `type`,`test1`.`t2`.`id` AS `id`,`test1`.`t2`.`uid` AS `uid`,`test1`.`t2`.`type` AS `type` from `test1`.`t1` join `test1`.`t2` where ((`test1`.`t1`.`uid` = 200009) and (`test1`.`t2`.`uid` = 200009)) |

-----------------+

1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值