zoukankan      html  css  js  c++  java
  • 记一次解决MYSQL占用CPU100%的问题

    有张表50W条数据,今天写了个JOB每隔10秒对其进行分组查询并获取每组的第一条数据。

    select 
        (
            select KVARH_Total 
            from e_monitorhours 
            where MobusId=a.MobusId
            order by Id DESC
            limit 1
        ) as KVARH_Total
    from e_monitorhours as a
    where 1=1 and MobusId not in (51,52,53)
    group by a.MobusId

    数据量小的时候还没问题,但是当达到几十万的时候,CPU100%。当然一开始是不知道的。

    后来在网上搜索说执行:

    show full processlist

    可以看到性能差的SQL语句,然后在info字段里面果断找到。

    因为MobusId没加索引,只要给其加上索引即可。


    空条件下查询千万级数据总数的方法:

    select TABLE_ROWS from information_schema.`TABLES` 
    WHERE table_schema='数据库名称' and TABLE_NAME = '表名';

    经过证实,这种方法查出来的结果是不对的。跟实际Count的数量不一致。原因可百度下。 

  • 相关阅读:
    hutool 解析 Excel
    上传文件
    Cannot construct instance of `com.**` (although at least one Creator exists)
    Java8之Optional
    java8之Stream
    java8之Lambda
    springboot+mybatis事务管理
    queryWrapper in like
    Java 组装 Tree
    JWT
  • 原文地址:https://www.cnblogs.com/subendong/p/12871469.html
Copyright © 2011-2022 走看看