首页 > 数据库 >mysql使用group by分类统计几万条以上数据处理

mysql使用group by分类统计几万条以上数据处理

时间:2022-10-08 13:57:30浏览次数:55  
标签:rrl group t1 worklocation mysql 数据处理 id select

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:rrl,group,t1,worklocation,mysql,数据处理,id,select
From: https://www.cnblogs.com/spll/p/16768698.html

相关文章

  • MySQL8.0.30安装教程
    第一步:输入MySQL网站       www.mysql.com  选择DOWNLOADS并点击进入:  选择最下面的“MySQL Community(GPL)Downloads>>”选项,进入下一页  选择......
  • MySQL面试题(四)
    38、  Myql 中的事务回滚机制概述事务是用户定义的一个数据库操作序列, 这些操作 要么全做要么全不做, 是一个不可分割的工作单位, 事务回滚是指将该事务已经完成的对数......
  • MySQL---视图
    视图常见的数据库对象概述为什么要使用视图?视图的理解 ......
  • 修改mysql 默认端口
    1.登录mysql-uroot-p密码2.查看端口showglobalvariableslike‘port’;3.修改端口编辑/etc/my.cnf文件,找到mysql配置文件my.cnf的port这一行,把之前的3306端口修......
  • MySQL 关闭源码解析
    前几天看大佬的公众号,发现一个有意思的问题,即平时应该如何关闭MySQL,如下几种方式,平时应该使用哪种呢:1.mysqladminshutdown2.servicemysqldstop3.kill'pidofmysq......
  • mac通过docker一键部署MySQL8
    目录mac通过docker一键部署MySQL8一、前言二、系统配置三、安装步骤Dockerhub查看镜像地址1、一键安装1.1、克隆脚本1.2、安装程序1.2.1、程序安装详情1.3、初始化用户1.3.......
  • mysql忘记密码的解决方案
    1.停止mysql服务;2.找到mysql的配置文件在最后一行添加skip-grant-tables=13.重新启用mysql并且用无账号模式进入mysql-uroot-p4.重新设置密码usemysql;up......
  • Ubuntu迁移mysql数据库到新的目录下
    1.先使用下面命令将mysql数据库服务停止:sudosystemctlstopmysql2.迁移到挂载新盘/mnt/data/方式一:sudomv/var/lib/mysql/mnt/data/方式二:sudocp-a/var/l......
  • MySQL
    MySQ基础知识与基本SQL语句MySQL字段类型、字符编码与配置文件主键与外键、自增、表关系之一对多、多对多、一对一MySQL查询关键字where、groupby、having、distinct、......
  • Linux上MySQL的卸载
    MySQL的卸载1、查看当前mysql安装状况rpm-qa|grepmysql#或yumlistinstall|grepmysql2、查看mysql的服务是否启动systemctlstatusmysqld2.1、如果......