MySQL 5.7 NOT EXISTS用法介绍

在MySQL中没有MINUS语句,要想实现MINUS语句,可以使用NOT EXISTS语句。

查看测试表中的数据
mysql> select * from person;
+----------------+-----------------+-----------------+
| MSISDN         | IMEI            | IMSI            |
+----------------+-----------------+-----------------+
| +3301000000000 | 129980000000000 | 208011000000000 |
| +3301000000001 | 129980000000100 | 208011000000001 |
| +3301000000002 | 129980000000200 | 208011000000002 |
| +3301000000003 | 129980000000300 | 208011000000003 |
| +3301000000004 | 129980000000400 | 208011000000004 |
| +3301000000005 | 129980000000500 | 208011000000005 |
| +3301000000006 | 129980000000600 | 208011000000006 |
| +3301000000007 | 129980000000700 | 208011000000007 |
+----------------+-----------------+-----------------+
8 rows in set (0.00 sec)
mysql> select * from card;
+-----------------+----------------+-----------------+
| IMSI            | MSISDN         | IMEI            |
+-----------------+----------------+-----------------+
| 208011000000000 | +3301000000000 | 129980000000000 |
| 208011000000001 | +3301000000001 | 129980000000100 |
| 208011000000002 | +3301000000002 | 129980000000200 |
| 208011000000003 | +3301000000003 | 129980000000300 |
| 208011000000004 | +3301000000004 | 129980000000400 |
| 208011000000005 | +3301000000005 | 129980000000500 |
| 208011000000006 | +3301000000006 | 129980000000600 |
| 208011000000007 | +3301000000007 | 129980000000700 |
| 208011000000008 | +3301000000008 | 129980000000800 |
| 208011000000009 | +3301000000009 | 129980000000900 |
+-----------------+----------------+-----------------+
10 rows in set (0.00 sec)

查询出在card中存在,但是在person中不存在的某一列MSISDN。
mysql> select * from card a where not exists(select * from person b where a.MSISDN=b.MSISDN);
+-----------------+----------------+-----------------+
| IMSI            | MSISDN         | IMEI            |
+-----------------+----------------+-----------------+
| 208011000000008 | +3301000000008 | 129980000000800 |
| 208011000000009 | +3301000000009 | 129980000000900 |
+-----------------+----------------+-----------------+
2 rows in set (0.00 sec)

查看执行计划
mysql> explain select * from card a where not exists(select * from person b where a.MSISDN=b.MSISDN);
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
| id | select_type        | table | partitions | type   | possible_keys | key     | key_len | ref           | rows | filtered | Extra                    |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
|  1 | PRIMARY            | a     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL          |   10 |   100.00 | Using where              |
|  2 | DEPENDENT SUBQUERY | b     | NULL       | eq_ref | PRIMARY       | PRIMARY | 20      | fire.a.MSISDN |    1 |   100.00 | Using where; Using index |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
2 rows in set, 2 warnings (0.00 sec)

通过执行计划,card表使用了全表扫描,person表使用了主键索引
mysql> show keys from card;
+-------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name             | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| card  |          0 | PRIMARY              |            1 | IMSI        | A         |          12 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
6 rows in set (0.00 sec)
mysql> show keys from person;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| person |          0 | PRIMARY  |            1 | MSISDN      | A         |           8 |     NULL | NULL   |      | BTREE      |         |               |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

由于两张表的主键不是相同列, 创建索引进行测试, 发现card表始终无法使用索引扫描
mysql> create index idx_card_msisdn on card(msisdn);
Query OK, 0 rows affected (0.22 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> explain select * from card a where not exists(select * from person b where a.MSISDN=b.MSISDN);
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
| id | select_type        | table | partitions | type   | possible_keys | key     | key_len | ref           | rows | filtered | Extra                    |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
|  1 | PRIMARY            | a     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL          |   10 |   100.00 | Using where              |
|  2 | DEPENDENT SUBQUERY | b     | NULL       | eq_ref | PRIMARY       | PRIMARY | 20      | fire.a.MSISDN |    1 |   100.00 | Using where; Using index |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
2 rows in set, 2 warnings (0.00 sec)

mysql> create index idx_card_msisdn_imsi on card(msisdn,imsi);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> explain select * from card a where not exists(select * from person b where a.MSISDN=b.MSISDN);
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
| id | select_type        | table | partitions | type   | possible_keys | key     | key_len | ref           | rows | filtered | Extra                    |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
|  1 | PRIMARY            | a     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL          |   10 |   100.00 | Using where              |
|  2 | DEPENDENT SUBQUERY | b     | NULL       | eq_ref | PRIMARY       | PRIMARY | 20      | fire.a.MSISDN |    1 |   100.00 | Using where; Using index |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
2 rows in set, 2 warnings (0.00 sec)

mysql> create index idx_card_imsi_msisdn on card(imsi,msisdn);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> explain select * from card a where not exists(select * from person b where a.MSISDN=b.MSISDN);
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
| id | select_type        | table | partitions | type   | possible_keys | key     | key_len | ref           | rows | filtered | Extra                    |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
|  1 | PRIMARY            | a     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL          |   10 |   100.00 | Using where              |
|  2 | DEPENDENT SUBQUERY | b     | NULL       | eq_ref | PRIMARY       | PRIMARY | 20      | fire.a.MSISDN |    1 |   100.00 | Using where; Using index |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
2 rows in set, 2 warnings (0.00 sec)

mysql> explain select * from card a where not exists(select * from person b where a.MSISDN=b.MSISDN);
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
| id | select_type        | table | partitions | type   | possible_keys | key     | key_len | ref           | rows | filtered | Extra                    |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
|  1 | PRIMARY            | a     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL          |   10 |   100.00 | Using where              |
|  2 | DEPENDENT SUBQUERY | b     | NULL       | eq_ref | PRIMARY       | PRIMARY | 20      | fire.a.MSISDN |    1 |   100.00 | Using where; Using index |
+----+--------------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+
2 rows in set, 2 warnings (0.00 sec)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26506993/viewspace-2123740/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26506993/viewspace-2123740/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值