(1)优化前
10年积累的成都网站设计、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有石龙免费网站建设让你可以放心的选择与我们合作。如下一条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 |另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。