1. Common selectType description
select_type query type |
illustrate |
SIMPLE |
Simple select query without using union and subquery |
PRIMARY |
The outermost select query |
SUBQUERY |
A subquery is included in select or where, the first select query in the subquery does not depend on the result set of the outer query |
DEPENDENT SUBQUERY |
The first select query in a subquery that depends on the result set of the outer query |
DERIVED |
Used when there are subqueries in the from clause. MySQL will recursively execute these subqueries, placing the results in temporary tables. |
UNION |
A second or subsequent select query in a UNION that does not depend on the result set of the outer query |
DEPENDENT UNION |
A second or subsequent select query in a UNION that depends on the result set of the outer query |
UNCACHEABLE UNION |
The second or subsequent select query in UNION is a non-cacheable subquery |
2. Examples of common selectType
- SIMPLE: simple select query, without using union and subquery
- PRIMARY: That is, the last executed statement
- SUBQUERY: contains subqueries in select or where
- DERIVED: Temporary tables will increase the burden on MYSQL, but sometimes they have to be used, analogy: two variables in Java exchange values
- UNION: The query results of two tables are merged
mysql> EXPLAIN SELECT t1.id from t1 UNION SELECT t2.id from t2;
+----+--------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+------+---------------+------+---------+------+------+-------+
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 3 | |
| 2 | UNION | t2 | ALL | NULL | NULL | NULL | NULL | 3 | |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+------+---------------+------+---------+------+------+-------+
- UNION RESULT: query again from the combined results
mysql> EXPLAIN SELECT t3.id from (SELECT t1.id from t1 UNION SELECT t2.id from t2)t3;
+----+--------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+------+---------------+------+---------+------+------+-------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 3 | |
| 2 | DERIVED | t1 | ALL | NULL | NULL | NULL | NULL | 3 | |
| 3 | UNION | t2 | ALL | NULL | NULL | NULL | NULL | 3 | |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+------+---------------+------+---------+------+------+-------+
3. SelectType usage summary:
- selectType meaning: the type of select query, mainly to distinguish between ordinary queries and complex queries such as joint queries and subqueries.
- selectType function: identify the type of query and adjust the query method
4. Executed SQL file
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');