(1)優(yōu)化前
10年積累的成都網(wǎng)站設(shè)計、做網(wǎng)站經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有石龍免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。如下一條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)另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享文章:Case:update中把in改寫成join性能提高數(shù)倍-創(chuàng)新互聯(lián)
URL地址:http://muchs.cn/article44/dgdhhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站設(shè)計公司、商城網(wǎng)站、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)