mysql not in 和 left join比较

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

mysql> select User.userName from User where userId not in (select userId from Article ); +----------+ | userName | +----------+ | xiaolin | +----------+ 1 row in set (0.03 sec)

mysql> explain select User.userName from User where userId not in (select userId from Article ); +----+--------------------+---------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+---------+------+---------------+------+---------+------+------+-------------+ | 1 | PRIMARY | User | ALL | NULL | NULL | NULL | NULL | 4 | Using where | | 2 | DEPENDENT SUBQUERY | Article | ALL | NULL | NULL | NULL | NULL | 11 | Using where | +----+--------------------+---------+------+---------------+------+---------+------+------+-------------+ 2 rows in set (0.00 sec)

mysql> select cc.userName from (select User.*,Article.userId as tempcolum from User left join Article on User.userId=Article.userId) as cc where cc.tempcolum is null; +----------+ | userName | +----------+ | xiaolin | +----------+ 1 row in set (0.02 sec)

mysql> explain select cc.userName from (select User.*,Article.userId as tempcolum from User left join Article on User.userId=Article.userId) as cc where cc.tempcolum is null; +----+-------------+------------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+------+---------------+------+---------+------+------+-------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 12 | Using where | | 2 | DERIVED | User | ALL | NULL | NULL | NULL | NULL | 4 | | | 2 | DERIVED | Article | ALL | NULL | NULL | NULL | NULL | 11 | | +----+-------------+------------+------+---------------+------+---------+------+------+-------------+

转载于:https://my.oschina.net/u/242853/blog/170455

你可能感兴趣的:(mysql not in 和 left join比较)