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
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @   下课后我要去放牛  阅读(635)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2018-10-08 java连接CentOS7上的redis
点击右上角即可分享
微信分享提示