Case:MySQL使用in带子查询的时候,子查询最好不要使用union或union all
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,MySQL使用in带子查询的时候,子查询不要使用union或union all特别是当外部表比较大的时候,千万不要使用in和union搭配,因为子查询中一旦使用union,执行计划会出现depende
千家信息网最后更新 2025年11月07日Case:MySQL使用in带子查询的时候,子查询最好不要使用union或union all
MySQL使用in带子查询的时候,子查询不要使用union或union all
特别是当外部表比较大的时候,千万不要使用in和union搭配,因为子查询中一旦使用union,执行计划会出现dependent subquery这种情况,
在生产上我们有使用类似的情况,导致SQL执行效率很差,下面举例说明,为了生产安全隐私,以下举例用测试表演示,原理相通。
举例
(1) 使用in和union搭配的时候,s表作为外部表,全表扫描,有260w行,执行20多秒。
mysql> select s.* from salaries s where s.emp_no in (select emp_no from employees e where e.first_name='Georgi' union all select emp_no from employees e where e.hire_date='1992-12-18');2718 rows in set (21.14 sec)mysql> desc select s.* from salaries s where s.emp_no in (select emp_no from employees e where e.first_name='Georgi' union all select emp_no from employees e where e.hire_date='1992-12-18');+----+--------------------+-------+------------+--------+---------------+---------+---------+------+---------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+--------------------+-------+------------+--------+---------------+---------+---------+------+---------+----------+-------------+| 1 | PRIMARY | s | NULL | ALL | NULL | NULL | NULL | NULL | 2612229 | 100.00 | Using where || 2 | DEPENDENT SUBQUERY | e | NULL | eq_ref | PRIMARY | PRIMARY | 4 | func | 1 | 10.00 | Using where || 3 | DEPENDENT UNION | e | NULL | eq_ref | PRIMARY | PRIMARY | 4 | func | 1 | 10.00 | Using where |+----+--------------------+-------+------------+--------+---------------+---------+---------+------+---------+----------+-------------+3 rows in set, 1 warning (0.00 sec)
(2)可以使用join来转化,再来看执行计划e表变成外表,s表使用PK检索,执行只要了0.32秒,效率大大提高。
mysql> select s.* from salaries s join (select emp_no from employees e where e.first_name='Georgi' union all select emp_no from employees e where e.hire_date='1992-12-18')e on s.emp_no=e.emp_no;2718 rows in set (0.32 sec)mysql> desc select s.* from salaries s join (select emp_no from employees e where e.first_name='Georgi' union all select emp_no from employees e where e.hire_date='1992-12-18')e on s.emp_no=e.emp_no;+----+-------------+------------+------------+------+----------------+---------+---------+----------+--------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+------------+------------+------+----------------+---------+---------+----------+--------+----------+-------------+| 1 | PRIMARY || NULL | ALL | NULL | NULL | NULL | NULL | 59866 | 100.00 | NULL || 1 | PRIMARY | s | NULL | ref | PRIMARY,emp_no | PRIMARY | 4 | e.emp_no | 9 | 100.00 | NULL || 2 | DERIVED | e | NULL | ALL | NULL | NULL | NULL | NULL | 299335 | 10.00 | Using where || 3 | UNION | e | NULL | ALL | NULL | NULL | NULL | NULL | 299335 | 10.00 | Using where |+----+-------------+------------+------------+------+----------------+---------+---------+----------+--------+----------+-------------+4 rows in set, 1 warning (0.00 sec)
时候
查询
情况
效率
生产
带子
安全
原理
外表
隐私
检索
测试
演示
最好
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发与测试基础知识点
黄埔服务器运维服务哪个品牌好
网络安全教育倡议书800字
自学数据库可以吗
网络安全认知升级
网络安全协会申请书
上海网络技术市场价
中国20大软件开发公司排名
天涯明月刀还有几个服务器
女生适合学网络安全与执法吗
数据库分区怎么添加
委托软件开发合同 百度
百度云视频服务器有点忙
西交网络安全与智能实验室
建党百年网络安全保障
连接不上服务器安全组设置
网络安全法第一讲讲的是什么
华为公司软件开发工程师待遇
assces数据库采集
visio服务器草稿位置
腾讯视频网络服务器是多少
数据库系统中数据的要求
ibm 服务器培训
字典攻击网络安全案例
软件开发版本分类
东城区网络技术市场报价
信息系统软件开发监理大纲
大数据数据库技术
微软数据库占有率
数据库管理岗面试题