记录一下mysql查询数据慢的调优过程

做项目过程中遇到了一些查询很慢的SQL语句,本次记录一下处理问题的过程及方法

如何定位到返回数据慢的问题?

1、使用谷歌浏览器查看接口请求时间的组成

查看Watting(TTFB)的时间,如果时间过程则是定位到后端服务处理问题时间较长!
Watting(TTFB)解析参考
定位TTFB问题

2、定位到执行的sql语句

慢SQL语句

3、进行SQL语句分析,使用explain关键字
explain
SELECT co.orderId,
       co.type,
       co.way,
       co.search,
       co.dept,
       co.orderProcess,
       co.pharmacyId,
       ud.name                               as doctorName,
       up.name                               as launchName,
       co.name,
       co.sex,
       co.age,
       co.phone,
       co.allergy,
       co.guardianName,
       co.guardianPhone,
       co.guardianIdCard,
       co.downloadUrl,
       co.consultProcess,
       co.recipelProcess,
       co.dispenseProcess,
       co.processContent,
       (SELECT us.name
        FROM user_staff us
        WHERE us.userId = co.applyUserId)    as applyName,
       co.applyDate,
       (SELECT us.name
        FROM user_staff us
        WHERE us.userId = co.auditUserId)    as auditName,
       co.auditDate,
       (SELECT us.name
        FROM user_staff us
        WHERE us.userId = co.dispenseUserId) as dispenseName,
       co.dispenseDate,
       co.printNum
FROM consult_order co
         LEFT JOIN user_doctor ud
                   ON ud.userId = co.targetEasemob
         LEFT JOIN user_pharmacy up
                   ON up.userId = co.pharmacyId
WHERE co.consultProcess = 4
  AND co.recipelProcess = 0
  AND DATE_FORMAT(co.applyDate, '%Y-%m-%d') >= '2021-05-31'
  AND DATE_FORMAT(co.applyDate, '%Y-%m-%d') <= '2021-05-31'
  AND co.way <> 0
ORDER BY co.applyDate DESC;
分析结果

SQL分析结果
type为sql执行过程中使用索引的重要指标,结果值从好到坏依次是:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

一般来说,得保证查询至少达到range级别,最好能达到ref。

4、建立索引

建立索引可以使用Navicat或者DataGrip等数据库工具!
建立索引的SQL参考

create index consult_order_orderId_index
	on consult_order (orderId);
5、建立索引后继续使用explain来分析SQL语句执行type的类别,索引建立好以后继续回到页面查询将会有很大提升
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是福强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值