背景:

分页卡顿接近一分钟,slow sql 显示 select count(0)  的时候耗时25秒,inner join order by 一堆问题,索引走了,explain 显示row还有6w多。

解决方案:

1、分页插件里,不再查询总数,如PageHelper.startPage(,,false);

2、对应list的sql,在mapper.xml里,自己手搓一个xxx_count,如原来是queryAbcList查询列表,那么需要在写一个queryAbcList_ COUNT方法,这里就可以很好的发挥了,order by ,inner join 都去掉;

3、多线程,如下图:

//定义线程数
        int subThreadNum = 2;
        CountDownLatch countDownLatch = new CountDownLatch(subThreadNum);
final list = null;
        final total= {0};
        for (final int[] i = {0}; i[0] < subThreadNum; i[0]++) {
            logger.info("i[0],run:{}",i[0]);
            if(i[0]==0){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                           list列表查询
                    } catch (Exception e) {
                       
                    } finally {
                        //线程结束时,将计时器减一
                        countDownLatch.countDown();
                       
                    }
                }}).start();
            }else if(i[0]==1){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                          total金额查询
                    } catch (Exception e) {
                       
                    } finally {
                        //线程结束时,将计时器减一
                        countDownLatch.countDown();
                      
                    }
                }}).start();
            }
        }
        //等待所有的子线程结束
        countDownLatch.await();

我们将插件里的list和total拆开,利用多线程,从代码层面,解决卡顿。

当然如果索引能解决最好,解决不了使用如上方法。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐