easyexcel导入百万级数据_记一次真百万级数据优化

本文探讨了在统计图界面优化中,如何处理和优化百万级数据的导入,通过EasyExcel进行高效处理。文章提到数据可以分为'冷数据',并分享了SQL优化的经验,强调了WHERE条件的筛选顺序影响以及避免使用导致笛卡尔积的IN操作。通过优化,显著减少了数据库的扫描行数。
摘要由CSDN通过智能技术生成

本文场景:统计图界面优化:

ad3a21b52e0138a8dfd72fcf8f1cf4f7.png
1:统计界面:适当加入缓存;
2:设备统计界面:
条形图: 这是原代码
$fill_res = $eqpt->field('count(EqptId) as count,date_format(CreateDate,"%y") as CreateDate')->where($where)->group('CreateDate')->buildSql();
$fill_res=Db::query("select sum(a.count) as count,a.CreateDate as CreateDate from $fill_res as a GROUP by CreateDate");

思考1:我们想一下,新增的数据去年是否已经是固定的?
然后这些我们可以把他们成为“冷数据"

$redis = new Predis();
$cache_key = md5(json_encode($where));
// redis 实现统计图冷热数据分离
if(!$redis->getHash($cache_key)){
    $fill_res = $eqpt->field('count(EqptId) as count,date_format(CreateDate,"%y") as CreateDate')->where($where)->group('CreateDate')->buildSql();
    $fill_res=Db::query("select sum(a.count) as count,a.CreateDate as CreateDate from $fill_res as a GROUP by CreateDate");
    $list_data = [];
    $list_count = count($fill_res)- 1;
    foreach ($fill_res as $key=>$val){
        if ($key < $list_count){
            $list_data[$val['CreateDate']] = $val['count'];
        }
    }
    $redis->setHash($cache_key,$list_data);
}else{
    $search_where = $where;
    $search_where['CreateDate'] = ['EGT',date("Y-01-01",strtotime("this year"))];
    $current_year_count = $eqpt->where($search_where)->count();
    $fill_list = $redis->getHash($cache_key);
    $fill_res = [];
    foreach ($fill_list as $k=>$value){
        $fill_res[] = ['CreateDate'=>$k,'count'=>$value];
    }
    $fill_res[] = ['CreateDate'=>date("Y"),"count"=>$current_year_count];
}

代码很烂;毕竟菜鸟大佬勿喷;
2:sql优化毕竟explain必不可少

此处附上一篇比较齐全的文章:

MySQL Explain详解 - 杰克思勒(Jacksile) - 博客园​www.cnblogs.com

3:注意点:where条件是从右往左依次筛选的;尽量在末尾就过滤大量数据

6b23e9146963aa0ef8a8d156886a820c.png

这条sql一看就很长;而且wherein太长了 wherein 就是笛卡尔积
咋:来搞搞呗

59402e31d67a51c5f2bfd0d861d02db4.png

不看不知道一看吓一跳有核扫描行数太多了~,减少

dd838a2aebacaa93ef363ce971d4c93f.png

这个表的扫描行数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值