mysql使用group by分类统计几万条以上数据处理
mysql如果group by分类统计几万条以上数据太慢,因为条件查询会走全表搜索
使用explain 可以看到自己的sql效率问题出现在哪,如:它查了7万多条数据才把结果查出来
sql优化后: 使用select * from ( select xxx from table_name1 group by xxx) 这样优化就快多了,先把结果集查出,再运用条件查询去过滤结果
select rbw.location_name as locationName, t1.worklocation_id as worklocationId, ( select count(rrl.id) from t_b_robot_record_logrecord rrl where rrl.status = 1 and rrl.station_id = '1567701277018120194' and rrl.errorcode = 1 and rrl.worklocation_id = t1.worklocation_id ) as errorcode1, ( select count(rrl.id) from t_b_robot_record_logrecord rrl where rrl.status = 1 and rrl.station_id = '1567701277018120194' and rrl.errorcode = 2 and rrl.worklocation_id = t1.worklocation_id ) as errorcode2, ( select count(rrl.id) from t_b_robot_record_logrecord rrl where rrl.status = 1 and rrl.station_id = '1567701277018120194' and rrl.errorcode = 3 and rrl.worklocation_id = t1.worklocation_id ) as errorcode3 from ( select worklocation_id from t_b_robot_record_logrecord group by worklocation_id ) t1 left join t_b_robot_base_worklocation rbw on rbw.id = t1.worklocation_id where t1.worklocation_id is not null
。
分类:
mysql
, 项目中的一些有用笔记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2018-10-08 java连接CentOS7上的redis