INSERT INTO `table_b` (`code`, `name`) VALUES
(1001, '测试1'),
(1002, '测试2');
好了,数据准备完成,下面可以做测试了
首先,我们做一个简单的左联接查询:
mysql> SELECT count(1) FROM `table_a` a LEFT JOIN table_b b ON a.code = b.code WHERE b.code =1001;
+----------+
| count(1) |
+----------+
| 2 |
+----------+
1 row in set, 2 warnings (0.00 sec)
因为数据量很少,所以查询几乎不耗时,我们来看一下explain的结果:
mysql> explain SELECT count(1) FROM `table_a` a LEFT JOIN table_b b ON a.code = b.code
WHERE b.code =1001;
+----+-------------+-------+------+---------------+------+---------+-------+------+------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+------------------------------------------------+
| 1 | SIMPLE | b | ref | code | code | 4 | const | 1 | Using where; Using index |
| 1 | SIMPLE | a | ALL | code | NULL | NULL | NULL | 6 | Range checked for each record (index map: 0x2) |
+----+-------------+-------+------+---------------+------+---------+-------+------+------------------------------------------------+
2 rows in set (0.00 sec)
mysql> explain SELECT count(1) FROM `table_a` a LEFT JOIN table_b b ON a.code = convert(b.code, char)
WHERE b.code =1001;
+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
| 1 | SIMPLE | b | ref | code | code | 4 | const | 1 | Using where; Using index |
| 1 | SIMPLE | a | ref | code | code | 62 | const | 2 | Using where; Using index |
+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
2 rows in set (0.00 sec)
可以看到key,ref都不再是NULL了,rows的数量也比之前的要少得多,索引已经生效。
当然,用cast也是一样的,如下:
mysql> explain SELECT count(1) FROM `table_a` a LEFT JOIN table_b b ON a.code = cast(b.code as char)
WHERE b.code =1001;
+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
| 1 | SIMPLE | b | ref | code | code | 4 | const | 1 | Using where; Using index |
| 1 | SIMPLE | a | ref | code | code | 62 | const | 2 | Using where; Using index |
+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
2 rows in set (0.00 sec)
上面的转换是将b表中的字段类型转成跟a表的一样,如果反过来,将a表的转成跟b表的一样,会有效果么?
于是,测试了如下的语句:
mysql> explain SELECT count(1) FROM `table_a` a LEFT JOIN table_b b ON convert(a.code, signed) = b.code
WHERE b.code =1001;
+----+-------------+-------+-------+---------------+------+---------+-------+------+---------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+-------+------+---------------------------------------------+
| 1 | SIMPLE | b | ref | code | code | 4 | const | 1 | Using where; Using index |
| 1 | SIMPLE | a | index | NULL | code | 62 | NULL | 6 | Using where; Using index; Using join buffer |
+----+-------------+-------+-------+---------------+------+---------+-------+------+---------------------------------------------+
2 rows in set (0.00 sec)