数据库cpu很高解决方案

问题
在这里插入图片描述
在这里插入图片描述
通过排查发现mysql的cpu和内存高是由于大量的性能差的垃圾sql导致的,由于签到查询量多
所以导致cpu高

修改方案
首先修改sql提高查询性能
修改前

select * from att_record where username='ding2' and address is not null and swipe_type = 1
            AND createtime BETWEEN '2022-09-02 00:00:00'
		AND '2022-09-02 23:59:59' 
       order by
        swipe_time limit 0,1

查询时间11秒
添加了address索引 和精简了查询列

select address from att_record where username='ding2' and address is not null and swipe_type = 1
            AND createtime BETWEEN '2022-09-02 00:00:00'
		AND '2022-09-02 23:59:59' 
        limit 0,1

耗时0.36

代码层面通过异步方式执行
由于子线程无法获取父线程的request对象中的参数
所以把参数读出来在传到线程池
线程池执行逻辑

        CompletableFuture voidCompletableFuture = CompletableFuture.runAsync(() -> {
        logger.info("线程号为***" + Thread.currentThread().getId()+"线程名称..."+Thread.currentThread().getName());
     //	签到逻辑
    }, threadPoolTaskExecutor);
@Configuration
public class PollConfig {
    @Bean
    public ThreadPoolTaskExecutor selfThreadPoolExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置线程名称
        executor.setThreadNamePrefix("completableFuture--pool");
        //设置最大线程数
        executor.setMaxPoolSize(50);
        //设置核心线程数
        executor.setCorePoolSize(10);
        //设置线程空闲时间,默认60
        executor.setKeepAliveSeconds(60);
        //设置队列容量
        executor.setQueueCapacity(1000);

        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                // .....任务过多时,存入数据库,定时执行,还是...
            }
        });
        // 使用预定义的异常处理类
        // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        return executor;
    }

}

//主类开启才可以
@EnableAsync

最后压测结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晴天M雨天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值