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');