溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Case:update中把in改寫成join性能提高數(shù)倍

發(fā)布時間:2020-07-01 04:26:44 來源:網(wǎng)絡(luò) 閱讀:644 作者:Darren_Chen 欄目:MySQL數(shù)據(jù)庫

(1)優(yōu)化前

如下一條SQL,把從1985-05-21入職前的員工薪資都增加500,執(zhí)行約20.70 s,

從執(zhí)行計劃中可以看出對表salaries進行的是索引全掃描,掃描行數(shù)約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)優(yōu)化后

把in改寫成join后,雖然對employees是全表掃描,但是掃描行數(shù)近29W行,大大減少,所以SQL執(zhí)行時間可以縮減到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     | <derived2> | 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)


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI