正如您所看到的,两个表具有相同的结构,但是一个左外连接使用键而另一个没有,因此在第二种情况下性能太差。任何解释为什么第二个左连接不使用键?
mysql> show create table t1;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`certificateNo` varchar(32) DEFAULT NULL,
UNIQUE KEY `certificateNo` (`certificateNo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table t2;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`certificateNo` varchar(32) CHARACTER SET utf8 DEFAULT NULL,
UNIQUE KEY `certificateNo` (`certificateNo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select count(1) from t1 left outer join t2 on (t1.certificateNo = t2.certificateNo) where t2.certificateNo is null;
+----------+
| count(1) |
+----------+
| 10839 |
+----------+
1 row in set (0.00 sec)
mysql> explain select count(1) from t1 left outer join t2 on (t1.certificateNo = t2.certificateNo) where t2.certificateNo is null;
+----+-------------+-------+-------+---------------+---------------+---------+------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------------+---------+------+-------+--------------------------+
| 1 | SIMPLE | t1 | index | NULL | certificateNo | 35 | NULL | 35691 | Using index |
| 1 | SIMPLE | t2 | ref | certificateNo | certificateNo | 99 | func | 138 | Using where; Using index |
+----+-------------+-------+-------+---------------+---------------+---------+------+-------+--------------------------+
2 rows in set (0.01 sec)
mysql> explain select count(1) from t2 left outer join t1 on (t1.certificateNo = t2.certificateNo) where t1.certificateNo is null;
+----+-------------+-------+-------+---------------+---------------+---------+------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------------+---------+------+-------+--------------------------+
| 1 | SIMPLE | t2 | index | NULL | certificateNo | 99 | NULL | 27785 | Using index |
| 1 | SIMPLE | t1 | index | NULL | certificateNo | 35 | NULL | 35691 | Using where; Using index |
+----+-------------+-------+-------+---------------+---------------+---------+------+-------+--------------------------+
2 rows in set (0.00 sec)
mysql> select count(1) from t1 inner join t2 on (t1.certificateNo = t2.certificateNo) ;
+----------+
| count(1) |
+----------+
| 21603 |
+----------+
1 row in set (0.16 sec)
mysql> select count(1) from t1;
+----------+
| count(1) |
+----------+
| 32442 |
+----------+
1 row in set (0.01 sec)
mysql> select count(1) from t2;
+----------+
| count(1) |
+----------+
| 26837 |
+----------+
1 row in set (0.01 sec)