mysql查询数据库desc_MySQL数据库之记一次order by desc limit导致的查询慢

本文记录了一次由于使用order by desc limit导致的MySQL查询缓慢问题,通过分析和优化,最终解决了问题。文章详细介绍了优化过程,包括去掉子查询、替换order by desc limit为max函数,从而显著提升了查询速度。
摘要由CSDN通过智能技术生成

本文主要向大家介绍了MySQL数据库之记一次order by desc limit导致的查询慢 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

d15305ecab7dc1fdd996063917f1d030.png

昨天接到一个客户的问题,电脑上可以打开网站,在手机上确不能打开报500的错。首先登陆上客户的服务器查看环境apache+mysql+php,php和mysql的占用都比较高,按经验来说那就是mysql的问题了,登陆mysql用show processlist查看进程,发现一条查询一直在sending datemysql> show processlist;+------+------+-----------------+--------+---------+------+--------------+------------------------------------------------------------------------------------------------------+| Id   | User | Host            | db     | Command | Time | State        | Info                |+------+------+-----------------+--------+---------+------+--------------+------------------------------------------------------------------------------------------------------+| 1832 | root | localhost:53490 | NULL   | Query   |    0 | NULL         | showprocesslist                || 1842 | root | localhost:53508 | yungou | Query   |    4 | Sending data | select a.id,a.q_user,a.q_showtime,a.thumb,a.title,a.q_uid,qishu,announced_type,q_end_time ,(SELECT ` |+------+------+-----------------+--------+---------+------+--------------+------------------------------------------------------------------------------------------------------+2 rows in set (0.00 sec)

明显不对啊,把这条语句单独拿出来执行也是慢得要死48s。

explain分析一下这条语句:mysql> explain select a.id,a.q_user,a.q_showtime,a.thumb,a.title,a.q_uid,qishu,announced_type,q_end_time ,(SELECT `time` FROM `go_member_go_record` WHERE shopid = a.id ORDER BY `time` DESC LIMIT 1 ) as gm_time from `go_shoplist` as a where`shenyurenshu` <=0 ORDER BY `gm_time` DESC LIMIT 4\G;*************************** 1. row ***************************           id: 1  select_type: PRIMARY        table: a         type: rangepossible_keys: shenyurenshu          key: shenyurenshu      key_len: 4          ref: NULL         rows: 945        Extra: Using where; Using filesort*************************** 2. row ***************************           id: 2  select_type: DEPENDENT SUBQUERY        table: go_member_go_record         type: indexpossible_keys: shopid          key: time      key_len: 63          ref: NULL         rows: 1        Extra: Using where2 rows in set (0.00 sec)

一看type是range就悲剧了,慢是肯定的了,但是945条记录也不至于这么慢啊!!

没办法用简化法来定位错误,先去掉子查询,直接select a.id,a.q_user,a.q_showtime,a.thumb,a.title,a.q_uid,qishu,announced_type,q_end_time from `go_shoplist` as a where `shenyurenshu` <=0 ORDER BY `gm_time` DESC LIMIT 4;

没有问题,速度杠杠的~~

那就是子查询的问题了哦,从哪里动刀呢?order by desc limit

首先去掉排序,查询杠杠的~~

然后想想order by desc limit 1怎么替代呢,这句的意思是查询最大的值,那我直接用max可以吗?马上修改select a.id,a.q_user,a.q_showtime,a.thumb,a.title,a.q_uid,qishu,announced_type,q_end_time ,(SELECT MAX(`time`) FROM `go_member_go_record` WHERE shopid = a.id ) as gm_time from `go_shoplist` as a where `shenyurenshu` <=0 ORDER BY `gm_time` DESC LIMIT 4;

打开网站,哈哈,速度杠杠的~~

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值