Case:update中把in改写成join性能提高数倍
admin
2023-04-20 16:04:38
0

(1)优化前

如下一条SQL,把从1985-05-21入职前的员工薪资都增加500,执行约20.70 s,

从执行计划中可以看出对表salaries进行的是索引全扫描,扫描行数约260W行。

mysql> update salaries set salary=salary+500 where emp_no in (select emp_no from employees where hire_date<='1985-05-21');
Query OK, 151583 rows affected (20.70 sec)
Rows matched: 151583  Changed: 151583  Warnings: 0

mysql> desc update salaries set salary=salary+500 where emp_no in (select emp_no from employees where hire_date<='1985-05-21');
+----+--------------------+-----------+------------+-----------------+---------------+---------+---------+------+---------+----------+-------------+
| id | select_type        | table     | partitions | type            | possible_keys | key     | key_len | ref  | rows    | filtered | Extra       |
+----+--------------------+-----------+------------+-----------------+---------------+---------+---------+------+---------+----------+-------------+
|  1 | UPDATE             | salaries  | NULL       | index           | NULL          | PRIMARY | 7       | NULL | 2674458 |   100.00 | Using where |
|  2 | DEPENDENT SUBQUERY | employees | NULL       | unique_subquery | PRIMARY       | PRIMARY | 4       | func |       1 |    33.33 | Using where |
+----+--------------------+-----------+------------+-----------------+---------------+---------+---------+------+---------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)


(2)优化后

把in改写成join后,虽然对employees是全表扫描,但是扫描行数近29W行,大大减少,所以SQL执行时间可以缩减到7.26s.

mysql> update salaries s join (select distinct e.emp_no from employees e where e.hire_date<='1985-05-21') e on s.emp_no=e.emp_no
    -> set s.salary=salary+500;
Query OK, 151583 rows affected (7.26 sec)
Rows matched: 151583  Changed: 151583  Warnings: 0

mysql> desc update salaries s join (select distinct e.emp_no from employees e where e.hire_date<='1985-05-21') e on s.emp_no=e.emp_no
    -> set s.salary=salary+500;
+----+-------------+------------+------------+------+----------------+---------+---------+----------+--------+----------+-------------+
| id | select_type | table      | partitions | type | possible_keys  | key     | key_len | ref      | rows   | filtered | Extra       |
+----+-------------+------------+------------+------+----------------+---------+---------+----------+--------+----------+-------------+
|  1 | PRIMARY     |  | NULL       | ALL  | NULL           | NULL    | NULL    | NULL     |  99827 |   100.00 | NULL        |
|  1 | UPDATE      | s          | NULL       | ref  | PRIMARY,emp_no | PRIMARY | 4       | e.emp_no |     10 |   100.00 | NULL        |
|  2 | DERIVED     | e          | NULL       | ALL  | PRIMARY        | NULL    | NULL    | NULL     | 299512 |    33.33 | Using where |
+----+-------------+------------+------------+------+----------------+---------+---------+----------+--------+----------+-------------+
3 rows in set, 1 warning (0.00 sec)


相关内容

热门资讯

玩家最新攻略“玄龙炸/金/花究... 家人们!今天小编来为大家解答玄龙炸/金/花透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪...
【第一消息】“新九天牛牛开挂器... 【第一消息】“新九天牛牛开挂器?”(太坑了果然有挂)您好,新九天牛牛这个游戏其实有挂的,确实是有挂的...
【今日要闻】“凑一桌游戏有没有... 有 亲,根据资深记者爆料凑一桌游戏是可以开挂的,确实有挂(咨询软件无需打...
东部战区发布微视频《这么近那么... 你就在我的舷窗下你就在我的舰艏前伸手可掬起日月潭水迈步可登上阿里山顶东部战区发布微视频《这么近那么美...
重磅消息“皇豪互娱牛牛是不是有... 网上科普关于“皇豪互娱牛牛有没有挂”话题很是火热,小编也是针对皇豪互娱牛牛作*弊开挂的方法以及开挂对...
玩家最新攻略“白金岛字牌集有没... 网上科普关于“白金岛字牌集有没有挂”话题很是火热,小编也是针对白金岛字牌集作*弊开挂的方法以及开挂对...
【第一资讯】“宝宝吃吃吃辅助器... 网上科普关于“宝宝吃吃吃有没有挂”话题很是火热,小编也是针对宝宝吃吃吃作*弊开挂的方法以及开挂对应的...
原创 如... 自从智能手机开始有“AI”这个概念以来,“情境感知”就成为了不少机型最明显能够让用户体会到AI“存在...
有色金属“涨”声一片,板块领跑... 近期,全球资本市场有色金属板块表现活跃,多种金属价格同步上涨,板块整体表现强劲。以白银为例,12月2...
玩家分享攻略“云梦天穹怎么装挂... 家人们!今天小编来为大家解答云梦天穹透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很...