Case:MySQL使用in带子查询的时候,子查询最好不要使用union或union all
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,MySQL使用in带子查询的时候,子查询不要使用union或union all特别是当外部表比较大的时候,千万不要使用in和union搭配,因为子查询中一旦使用union,执行计划会出现depende
千家信息网最后更新 2026年10月31日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安全错误
数据库的锁怎样保障安全
天津市摩尔互联网科技
亿人帮互联网科技有限公司
互联网科技深圳
河南真二互联网科技有限公司
与互联网科技公司
互联网科技排名
贷财互联网科技
凡科互联网科技
互联网科技logo
碳银互联网科技有限公司
中国互联网金融科技基金
科技赋能互联网健康
来此购互联网科技有限公司
荆州市昕露互联网络科技有限公司
未来五年的互联网科技
河南住房之家互联网科技
迪庆互联网科技招生
江苏苏星互联网科技有限公司
四川互联网展示科技馆设计
求路客互联网科技电话
百单网互联网科技有限公司怎么样
碳银互联网科技
互联网 科技服务平台
科技互联网大厂
燊美集互联网科技有限公司
广东工业互联网大赛深圳学泰科技
领航科技大厦互联网公司
图游互联网科技
银川新华互联网科技学校是国营吗
三亚科技创新互联网基地