试了一下,没看出什么区别,有索引都会走索引,没有都全表扫描。
另外 第二个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)