1.id相同代表:执行顺序由上到下
mysql> EXPLAIN SELECT t1.id from t1,t3,t2 where t1.id = t3.id and t3.id = t2.id and t2.id = 1;
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 1 | SIMPLE | t3 | ALL | NULL | NULL | NULL | NULL | 3 | Using where; Using join buffer |
| 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where; Using join buffer |
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
mysql> EXPLAIN SELECT t1.id from t1,t2,t3 where t1.id = t3.id and t3.id = t2.id and t2.id = 1;
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where; Using join buffer |
| 1 | SIMPLE | t3 | ALL | NULL | NULL | NULL | NULL | 3 | Using where; Using join buffer |
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
mysql> EXPLAIN SELECT * from t1,t2,t3 where t1.id = t2.id;
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------------------+
| 1 | SIMPLE | t2 | ALL | PRIMARY | NULL | NULL | NULL | 1 | |
| 1 | SIMPLE | t1 | eq_ref | PRIMARY | PRIMARY | 4 | mysql1.t2.id | 1 | |
| 1 | SIMPLE | t3 | ALL | NULL | NULL | NULL | NULL | 3 | Using join buffer |
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------------------+
mysql> EXPLAIN SELECT * from t1,t2 where t1.id = t2.id;
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------+
| 1 | SIMPLE | t2 | ALL | PRIMARY | NULL | NULL | NULL | 1 | |
| 1 | SIMPLE | t1 | eq_ref | PRIMARY | PRIMARY | 4 | mysql1.t2.id | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------+
mysql> EXPLAIN SELECT * from t1,t2 where t2.id = t1.id;
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------+
| 1 | SIMPLE | t2 | ALL | PRIMARY | NULL | NULL | NULL | 1 | |
| 1 | SIMPLE | t1 | eq_ref | PRIMARY | PRIMARY | 4 | mysql1.t2.id | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+--------------+------+-------+
2.id不同代表:如果是子查询,id越大的越先被执行,理解转化为数学运算:1+(1*(1+1))
mysql> EXPLAIN SELECT t2.`name` from t2 where t2.id =(SELECT t1.id from t1 where t1.`name`= 'downeyjr_1');
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 2 | SUBQUERY | t1 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
mysql> EXPLAIN SELECT t2.`name` from t2 where t2.id =(SELECT t1.id from t1 where t1.`name`= (SELECT t3.`name` from t3 where t3.id= 2));
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 2 | SUBQUERY | t1 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 3 | SUBQUERY | t3 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
3.id相同又不同代表:先执行id值大的,然后顺序执行id相同的
mysql> EXPLAIN SELECT t2.`name` from t2 ,(SELECT t3.* from t3 where t3.id= 2) t4 where t4.id = t2.id;
+----+-------------+------------+--------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
| 1 | PRIMARY | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 2 | DERIVED | t3 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
+----+-------------+------------+--------+---------------+------+---------+------+------+-------------+
mysql> EXPLAIN SELECT t4.`name` from (SELECT t3.* from t3 where t3.id= 2)t4,(SELECT t2.* from t2 where t2.id= 2)t5 where t4.id = t5.id ;
+----+-------------+------------+--------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
| 1 | PRIMARY | <derived3> | system | NULL | NULL | NULL | NULL | 1 | |
| 3 | DERIVED | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 2 | DERIVED | t3 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
+----+-------------+------------+--------+---------------+------+---------+------+------+-------------+
4.id使用总结:
- id意义:MySQL Query Optimizer 选定的执行计划中查询的序列号。表示查询中执行 select 子句或操作表的顺序,id 值越大优先级越高,越先被执行。id 相同,执行顺序由上至下。
- id作用:表的读取顺序,小表驱动大表时的微调
5.执行的SQL文件
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t1
-- ----------------------------
DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
`id` int(5) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t1
-- ----------------------------
INSERT INTO `t1` VALUES ('1', 'downeyjr_1');
INSERT INTO `t1` VALUES ('2', 'downeyjr_2');
INSERT INTO `t1` VALUES ('3', 'downeyjr_3');
-- ----------------------------
-- Table structure for t2
-- ----------------------------
DROP TABLE IF EXISTS `t2`;
CREATE TABLE `t2` (
`id` int(5) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t2
-- ----------------------------
INSERT INTO `t2` VALUES ('1', 'downeyjr_1');
INSERT INTO `t2` VALUES ('2', 'downeyjr_2');
INSERT INTO `t2` VALUES ('3', 'downeyjr_3');
-- ----------------------------
-- Table structure for t3
-- ----------------------------
DROP TABLE IF EXISTS `t3`;
CREATE TABLE `t3` (
`id` int(5) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t3
-- ----------------------------
INSERT INTO `t3` VALUES ('1', 'downeyjr_1');
INSERT INTO `t3` VALUES ('2', 'downeyjr_2');
INSERT INTO `t3` VALUES ('3', 'downeyjr_3');