mysql not in 和 left join比較

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

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)sql

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)table

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 | <derived2> | 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 | | +----+-------------+------------+------+---------------+------+---------+------+------+-------------+select

相關文章
相關標籤/搜索