当前位置: 首页 > news >正文

【mysql】explain执行计划之select_type列

目录

        • 一、说明
        • 二、示例
            • 2.1 simple:简单表,不使用union或者子查询
            • 2.2 primary:主查询,外层的查询
            • 2.3 subquery:select、where之后包含了子查询,在select语句中出现的子查询语句,结果不依赖于外部查询(不在from语句中)
            • 2.4 dependent subquery:指在select语句中出现的查询语句,结果依赖于外部查询
            • 2.5 derived:派生表
            • 2.6 union
            • 2.7 union all
            • 2.8 dependent union
        • 三、sql脚本

一、说明

  • 1.表示select的类型,查询语句执行的查询操作的类型
  • 2.常见的取值有:simple、primary、union、subquery、dependent subquery
  • 3.simple:简单表,不使用union或者子查询
  • 4.primary:主查询,外层的查询
  • 5.union:union中的第二个或者后面的查询语句
  • 6.subquery:select、where之后包含了子查询,在select语句中出现的子查询语句,结果不依赖于外部查询(不在from语句中)
  • 7.dependent subquery:指在select语句中出现的查询语句,结果依赖于外部查询
  • 8.derived:派生表,在from子句的查询语句,表示从外部数据源中推导出来,不是从select语句中的其他列中选择出来的
  • 9.union:分union与union all两种,若第二个select出现在union之后,则被标记为union;如果union被from子句的子查询包含,则第一个select会被标记为derived;union会针对相同的结果集进行去重,union all不会进行去重处理
  • 10.dependent union:当union作为子查询时,第一个union为dependent subquery,第二个union为dependent union

二、示例

2.1 simple:简单表,不使用union或者子查询
1.单表的select_type,不使用union和子查询
explain select * from users;

在这里插入图片描述

2.表连接查询的select_type,不使用union和子查询
explain select * from users inner join orders where users.id = orders.user_id;

在这里插入图片描述

2.2 primary:主查询,外层的查询
explain select id from users union select id from orders;

在这里插入图片描述

2.3 subquery:select、where之后包含了子查询,在select语句中出现的子查询语句,结果不依赖于外部查询(不在from语句中)
explain select orders.*,(select product_name from products where id = 10001) from orders;

在这里插入图片描述

2.4 dependent subquery:指在select语句中出现的查询语句,结果依赖于外部查询
explain select orders.*,(select product_name from products where id = orders.product_id) from orders;

在这里插入图片描述

2.5 derived:派生表
-- 关闭对衍生表合并优化
set session optimizer_switch='derived_merge=off';
-- 查看optimizer_switch参数
show variables like '%optimizer_switch%';
-- 
explain select * from (select user_id from orders where id = 10001) as temp;
-- 还原表合并优化
set session optimizer_switch='derived_merge=on';

在这里插入图片描述

2.6 union

1.union result:union关键字会将数据结果进行去重,会使用一个临时表,临时表的记录会被标记为union result

explain select * from (select id from products where product_price = 8000 union select id from orders where user_id = 20001 union select id from users where user_name = '张三' ) as temp;

在这里插入图片描述

2.7 union all
explain select * from (select id from products where product_price = 8000 union all select id from orders where user_id = 20001 union all select id from users where user_name = '张三' ) as temp;

在这里插入图片描述

2.8 dependent union
explain select * from orders where id in (select id from products where product_price = 8000 union all select id from orders where user_id = 20001
union all select id from users where user_name = '张三');

在这里插入图片描述

三、sql脚本

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for orders
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders`  (`id` int(0) NOT NULL COMMENT '主键id',`price` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '订单总额',`user_id` int(0) DEFAULT NULL COMMENT '用户id',`product_id` int(0) DEFAULT NULL COMMENT '产品id',`number` int(0) DEFAULT NULL COMMENT '产品数量',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of orders
-- ----------------------------
INSERT INTO `orders` VALUES (10001, '80000', 20001, 10001, 10);-- ----------------------------
-- Table structure for products
-- ----------------------------
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products`  (`id` int(0) NOT NULL COMMENT '主键id',`product_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '商品名称',`product_price` decimal(10, 2) DEFAULT NULL COMMENT '商品价格',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of products
-- ----------------------------
INSERT INTO `products` VALUES (10001, '苹果手机', 8000.00);-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`  (`id` int(0) NOT NULL COMMENT '主键id',`user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '用户名称',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (20001, '张三');SET FOREIGN_KEY_CHECKS = 1;
http://www.lryc.cn/news/69253.html

相关文章:

  • 网易云音乐开发--音乐播放暂停切换上下首功能实现
  • 如何学习网络安全?
  • 软件测试适合女生吗?
  • 华为云——代码托管的使用
  • ChatGPT从⼊⻔到精通
  • node + alipay-sdk 沙箱环境简单测试电脑网站支付
  • 卷积神经网络详解
  • API架构的选择,RESTful、GraphQL还是gRPC
  • 人机融合智能的测量、计算与评价
  • 虹科新品 | 高可靠性、可适用于高磁/压的线性传感器!
  • 支付系统设计五:对账系统设计01-总览
  • 阿里三面过了,却无理由挂了,HR反问一句话:为什么不考虑阿里?
  • 办公智慧化风起云涌,华为MateBook X Pro 2023是最短距离
  • 分布式项目 09.服务器之间的通信和三个工具类
  • C# 基本语法
  • 做网络爬虫需要掌握哪些技术?
  • 工作利器:三种简单方法将PPT转换成PDF
  • 《设计模式》状态模式
  • 2023年好用的设计图制作软件推荐
  • JavaNote_1.0.2_Spring
  • 微服务多模块:Springboot+Security+Redis+Gateway+OpenFeign+Nacos+JWT (附源码)仅需一招,520彻底拿捏你
  • HNU数据结构与算法分析-作业4-图结构
  • AMPL IDE语法整理
  • 从0-1搭建支持gb28181协议搭建流媒体平台
  • 数据结构与算法之栈: Leetcode 682. 棒球比赛 (Typescript版)
  • E-office Server_v9.0 漏洞分析
  • MySQL数据库,JDBC连接数据库操作流程详细介绍
  • libevent高并发网络编程 - 06_基于libevent的C++线程池实现
  • 【Java EE 初阶】线程安全及死锁解决方案
  • C语言函数大全-- _w 开头的函数(5)