在实现业务逻辑的时候,有些复杂一点逻辑会用数据库子查询去实现,但是sql用子查询会带来性能问题,下面就一个例子来说明,怎么优化子查询,来提升查询速度

mysql> desc update t_student_info a

-> set a.exstudentid='test01'

-> where a.studentID in

-> (select studentID from (select studentID from t_student_info where stdTYPE='8' and state=2 limit 10000,100) b);

+----+--------------------+----------------+------------+----------------+---------------+-------------+---------+------+--------+----------+-------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+--------------------+----------------+------------+----------------+---------------+-------------+---------+------+--------+----------+-------------+

| 1 | UPDATE | a | NULL | index | NULL | PRIMARY | 24 | NULL | 221058 | 100.00 | Using where |

| 2 | DEPENDENT SUBQUERY | | NULL | index_subquery | | | 24 | func | 221 | 100.00 | Using index |

| 3 | DERIVED | t_student_info | NULL | ALL | NULL | NULL | NULL | NULL | 221058 | 1.00 | Using where |

+----+--------------------+----------------+------------+----------------+---------------+-------------+---------+------+--------+----------+-------------+

3 rows in set (0.00 sec)

可以看到这个Update语句的执行计划,用的是DEPENDENT SUBQUERY,这样就需要循环的去执行这个只查询,效率会慢,能不能把这个只查询改一下,改成join查询呢,下面就是优化之后的sql写法

update t_student_info a set a.exstudentid='test01' where a.studentID in (select studentID from (select studentID from t_student_info where stdTYPE='8' and state=2 limit 10000,100) b)

mysql> desc update t_student_info a

-> inner join

-> (select studentID from t_student_info where stdTYPE='8' and state=2 limit 10000,100) b

-> on a.studentID=b.studentID

-> set a.exstudentid='test01';

+----+-------------+----------------+------------+--------+---------------+---------+---------+-------------+--------+----------+-------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+----------------+------------+--------+---------------+---------+---------+-------------+--------+----------+-------------+

| 1 | PRIMARY | | NULL | ALL | NULL | NULL | NULL | NULL | 2210 | 100.00 | NULL |

| 1 | UPDATE | a | NULL | eq_ref | PRIMARY | PRIMARY | 24 | b.studentID | 1 | 100.00 | NULL |

| 2 | DERIVED | t_student_info | NULL | ALL | NULL | NULL | NULL | NULL | 221058 | 1.00 | Using where |

+----+-------------+----------------+------------+--------+---------------+---------+---------+-------------+--------+----------+-------------+

3 rows in set (0.00 sec)

可以从执行计划中看到执行计划已经从DEPENDENT SUBQUERY变成了DERIVED,以驱动表去关联查询了,下面来看看实际执行效果

mysql> update t_student_info a set a.exstudentid='test01' where a.studentID in (select studentID from (select studentID from t_student_info where stdTYPE='8' and state=2 limit 10000,100) b);

Query OK, 0 rows affected (0.37 sec)

Rows matched: 100 Changed: 0 Warnings: 0

mysql> update t_student_info a set a.exstudentid='test01' where a.studentID in (select studentID from (select studentID from t_student_info where stdTYPE='8' and state=2 limit 10000,100) b);

Query OK, 0 rows affected (0.39 sec)

Rows matched: 100 Changed: 0 Warnings: 0

mysql> update t_student_info a inner join (select studentID from t_student_info where stdTYPE='8' and state=2 limit 10000,100) b on a.studentID=b.studentID set a.exstudentid='test01';

Query OK, 0 rows affected (0.07 sec)

Rows matched: 100 Changed: 0 Warnings: 0

mysql> update t_student_info a inner join (select studentID from t_student_info where stdTYPE='8' and state=2 limit 10000,100) b on a.studentID=b.studentID set a.exstudentid='test01';

Query OK, 0 rows affected (0.07 sec)

Rows matched: 100 Changed: 0 Warnings: 0

为了排除因为物理读导致的干扰,没条sql都连续执行2遍,从执行结果可以看到,使用子查询的sql平均执行时间在370毫秒,而用inner join的sql平均执行时间在70毫秒,效率提升了5倍多,优化效果还是很明显的,小伙伴可能会觉得,才有5倍提升,其实优化之后的语句耗费时间的在limit 10000,100这里,如果改外limit 1,100大家再来看看对比效果

mysql> update t_student_info a inner join (select studentID from t_student_info where stdTYPE='8' and state=2 limit 1,100) b on a.studentID=b.studentID set a.exstudentid='test01';

Query OK, 0 rows affected (0.00 sec)

Rows matched: 100 Changed: 0 Warnings: 0

mysql> update t_student_info a inner join (select studentID from t_student_info where stdTYPE='8' and state=2 limit 1,100) b on a.studentID=b.studentID set a.exstudentid='test01';

Query OK, 0 rows affected (0.00 sec)

Rows matched: 100 Changed: 0 Warnings: 0

mysql> update t_student_info a set a.exstudentid='test01' where a.studentID in (select studentID from (select studentID from t_student_info where stdTYPE='8' and state=2 limit 1,100) b);

Query OK, 0 rows affected (0.31 sec)

Rows matched: 100 Changed: 0 Warnings: 0

mysql> update t_student_info a set a.exstudentid='test01' where a.studentID in (select studentID from (select studentID from t_student_info where stdTYPE='8' and state=2 limit 1,100) b);

Query OK, 0 rows affected (0.31 sec)

Rows matched: 100 Changed: 0 Warnings: 0

inner join的执行时间已经是几毫秒了,而子查询还是在310毫秒,这效果就分明显,提升了100多倍,这种方法优化,不仅适合in,还是适合exists的优化

喜欢的同学可以关注我的公众号(db_arch)(Mysql数据库运维与架构设计)

喜欢的同学可以关注我的公众号(db_arch)(Mysql数据库运维与架构设计)

MySQL数值扩大一百倍_Mysql优化----一条SQL百倍提升之旅相关推荐

  1. mysql 走索引 很慢_MySQL 优化:为什么 SQL 走索引还那么慢?

    背景 2019-01-11 9:00-10:00 一个 MySQL 数据库把 CPU 打满了. 硬件配置:256G 内存,48 core 分析过程 接手这个问题时现场已经不在了,信息有限,所以我们先从 ...

  2. mysql最大并行用户设置_mysql 优化配置

    1.目的: 通过根据服务器目前状况,修改Mysql的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能. 2.服务器参数: 32G内存.4个CPU,每个CPU 8核. 3.MySQL目 ...

  3. Mysql价格降低20%应该怎么写_mysql优化20条原则

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...

  4. 哪位兄台能优化这条SQL语句,在线等,捉急!!!

    技术主管让我优化一条SQL语句,这条语句是购买过之后将余额减掉,将理财金额增加,参数money是在业务层的,之前以前有过安全方面的过滤了.但主管只说了一句,不可以这样直接加减,MMP,玩劳资哪?研究了 ...

  5. mysql update 几万 非常慢_Mysql优化专题

    优化,一直是面试最常问的一个问题.因为从优化的角度,优化的思路,完全可以看出一个人的技术积累.那么,关于系统优化,假设这么个场景,用户反映系统太卡(其实就是高并发),那么我们怎么优化? 如果请求过多, ...

  6. mysql in 命中索引_MySql优化-你的SQL命中索引了吗

    在项目开发中SQL是必不可少的,表索也一样.这些SQL的运行性能不知道吗?有多少是命中了索引的?命中哪个索引?索引中有哪个是无效索引?这些无效索引是否会影响系统的性能?带着这些问题我们一起来学习一下. ...

  7. mysql性能调优快捷键_mysql优化篇

    mysql优化篇 2019-4-12 hubo 数据库 优化目标 1.减少 IO 次数 IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所决定的,大部分数据库操作中超过90%的时间都是 IO 操作 ...

  8. mysql 设置按天分表_MySQL 优化实战记录

    阅读本文大概需要 2 分钟. 背景 本次SQL优化是针对javaweb中的表格查询做的. 部分网络架构图 业务简单说明 N个机台将业务数据发送至服务器,服务器程序将数据入库至MySQL数据库.服务器中 ...

  9. mysql 走索引 很慢_MySQL优化:为什么SQL走索引还那么慢?

    背景 2019-01-11 9:00-10:00 一个 MySQL 数据库把 CPU 打满了. 硬件配置:256G 内存,48 core 分析过程 接手这个问题时现场已经不在了,信息有限,所以我们先从 ...

最新文章

  1. 浅析校园安防视频监控设备发展趋势
  2. python学多久可以做项目-怎么自学python,大概要多久?
  3. shell 常用命令语句
  4. 黑客内参--浅谈DIV+CSS的优势
  5. 求最值(最大值和最小值)
  6. [Python人工智能] 三十二.Bert模型 (1)Keras-bert基本用法及预训练模型
  7. 单片机小白学步系列(二十三) IO口原理知识补充:双向IO口、互补推挽、高阻态
  8. C# 读写ini文件 保存信息
  9. power bi函数_在Power BI中的行上使用聚合函数
  10. 推荐一个接口文档工具
  11. 一文梳理水下目标检测方法
  12. 求二叉树上结点的路径c语言版,求二叉树根到给定节点的路径设计报告.doc
  13. shell如何传参?
  14. Direct3D学习笔记
  15. 从零开始用Python3做数据分析
  16. 数据结构题集c语言版题目与答案,数据结构题集(C语言版)答案 - 严蔚敏编著...
  17. python画正态分布_python 画正态曲线
  18. 小工具:找出序列中的极值点
  19. Type-c快充加音频芯片深度解析(LDR6023C)
  20. 富士康服务器linux运维,【2019IT运维十大样板工程】富士康云桌面及智能运维项目...

热门文章

  1. ACL 2018最佳论文公布!计算语言学最前沿研究都在这里了
  2. 阿里感悟(九)-如何才能晋升
  3. checkstyle安装使用
  4. 转载-Android数据库高手秘籍(一)——SQLite命令
  5. js实现的时间轴效果
  6. 64位windows与32位windows的区别
  7. zookeeper集群启动报错:Cannot open channel to * at election address /ip:3888
  8. 70+Python项目,面向初学者、中级和经验丰富的开发人员
  9. php仿微信底部菜单,Android实现简单底部导航栏 Android仿微信滑动切换效果
  10. Linux之SSH协议知识点总结