项目中统计SQL执行缓慢的方案-数据预处理

使用场景:
由于表数据量巨大,导致一些统计相关的sql执行非常慢,使用户有非常不好的体验,并且sql和数据库已经没有优化空间了。(并且该统计信息数据实时性要求不高的前提下)

解决方案:
整体思路:创建预处理表——通过定时任务将数据插入到结果表——统计信息时直接通过结果表进行查询——大大提高响应速度

注:1.结果表中需要包含查询条件里的所有字段

2.定时任务可以根据实际需要设定频率

3.最好创建一个与结果表表结构一样的临时表用于数据暂存,防止在插入数据这个时间段上导致结果表数据空白。(直接将临时表数据插入到结果表速度很快这段时间可以忽略)

方案示例:
第一步:创建结果表

drop table if exists `user_study_statistics`;
CREATE TABLE `project_statistics` (
  `project_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '项目id',
  `project_unit_id` int(11) NOT NULL COMMENT '单位工程id',
  `unit_name` varchar(255) DEFAULT NULL COMMENT '单位工程名称',
  `project_name` varchar(255) DEFAULT NULL COMMENT '项目名称',
  `file_count` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数',
  `file_count_30` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数(30天内)',
  `file_count_90` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数(90天内)',
  `file_count_180` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数(180天内)',
  `check_count` int(8) DEFAULT NULL COMMENT '检查次数',
  `check_count_30` int(8) DEFAULT NULL COMMENT '检查次数(30天内)',
  `check_count_90` int(8) DEFAULT NULL COMMENT '检查次数(90天内)',
  `check_count_180` int(8) DEFAULT NULL COMMENT '检查次数(180天内)',
  `check_num` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数',
  `check_num_30` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数(30天内)',
  `check_num_90` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数(90天内)',
  `check_num_180` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数(180天内)',
  `check_scale` varchar(26) DEFAULT NULL COMMENT '检查文件占比',
  `check_scale_30` varchar(26) DEFAULT NULL COMMENT '检查文件占比(30天内)',
  `check_scale_90` varchar(26) DEFAULT NULL COMMENT '检查文件占比(90天内)',
  `check_scale_180` varchar(26) DEFAULT NULL COMMENT '检查文件占比(180天内)',
  `pass_scale` varchar(26) DEFAULT NULL COMMENT '通过率',
  `pass_scale_30` varchar(26) DEFAULT NULL COMMENT '通过率(30天内)',
  `pass_scale_90` varchar(26) DEFAULT NULL COMMENT '通过率(90天内)',
  `pass_scale_180` varchar(26) DEFAULT NULL COMMENT '通过率(180天内)',
  `correct_count` int(8) DEFAULT NULL COMMENT '整改条数',
  `correct_count_30` int(8) DEFAULT NULL COMMENT '整改条数(30天内)',
  `correct_count_90` int(8) DEFAULT NULL COMMENT '整改条数(90天内)',
  `correct_count_180` int(8) DEFAULT NULL COMMENT '整改条数(180天内)',
  `out_time_count` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数',
  `out_time_count_30` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数(30天内)',
  `out_time_count_90` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数(90天内)',
  `out_time_count_180` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数(180天内)'
) ENGINE=InnoDB DEFAULT CHARSCREATE TABLE `project_statistics_tmp` (
  `project_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '项目id',
  `project_unit_id` int(11) NOT NULL COMMENT '单位工程id',
  `unit_name` varchar(255) DEFAULT NULL COMMENT '单位工程名称',
  `project_name` varchar(255) DEFAULT NULL COMMENT '项目名称',
  `file_count` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数',
  `file_count_30` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数(30天内)',
  `file_count_90` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数(90天内)',
  `file_count_180` int(8) NOT NULL DEFAULT '0' COMMENT '文件总数(180天内)',
  `check_count` int(8) DEFAULT NULL COMMENT '检查次数',
  `check_count_30` int(8) DEFAULT NULL COMMENT '检查次数(30天内)',
  `check_count_90` int(8) DEFAULT NULL COMMENT '检查次数(90天内)',
  `check_count_180` int(8) DEFAULT NULL COMMENT '检查次数(180天内)',
  `check_num` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数',
  `check_num_30` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数(30天内)',
  `check_num_90` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数(90天内)',
  `check_num_180` int(8) NOT NULL DEFAULT '0' COMMENT '检查文件总数(180天内)',
  `check_scale` varchar(26) DEFAULT NULL COMMENT '检查文件占比',
  `check_scale_30` varchar(26) DEFAULT NULL COMMENT '检查文件占比(30天内)',
  `check_scale_90` varchar(26) DEFAULT NULL COMMENT '检查文件占比(90天内)',
  `check_scale_180` varchar(26) DEFAULT NULL COMMENT '检查文件占比(180天内)',
  `pass_scale` varchar(26) DEFAULT NULL COMMENT '通过率',
  `pass_scale_30` varchar(26) DEFAULT NULL COMMENT '通过率(30天内)',
  `pass_scale_90` varchar(26) DEFAULT NULL COMMENT '通过率(90天内)',
  `pass_scale_180` varchar(26) DEFAULT NULL COMMENT '通过率(180天内)',
  `correct_count` int(8) DEFAULT NULL COMMENT '整改条数',
  `correct_count_30` int(8) DEFAULT NULL COMMENT '整改条数(30天内)',
  `correct_count_90` int(8) DEFAULT NULL COMMENT '整改条数(90天内)',
  `correct_count_180` int(8) DEFAULT NULL COMMENT '整改条数(180天内)',
  `out_time_count` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数',
  `out_time_count_30` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数(30天内)',
  `out_time_count_90` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数(90天内)',
  `out_time_count_180` int(8) DEFAULT NULL COMMENT '逾期未完成整改条数(180天内)'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='项目检查状态统计表';

ET=utf8 COMMENT='项目检查状态统计表_临时表';

创建视图:

CREATE VIEW `v_project_statistics_info` AS
SELECT project_id,project_unit_id,unit_name,project_name,
COUNT(1) file_count,-- 文件总数
COUNT((datediff(date(now()),date(gmt_create)) <=30 ) OR null) file_count_30,
COUNT((datediff(date(now()),date(gmt_create)) <=90 ) OR null) file_count_90,
COUNT((datediff(date(now()),date(gmt_create)) <=180 ) OR null) file_count_180,
SUM(check_count) as check_count,-- 检查次数
SUM(CASE WHEN (datediff(date(now()),date(modify_time)) <=30 ) THEN check_count ELSE 0 END) AS check_count_30,
SUM(CASE WHEN (datediff(date(now()),date(modify_time)) <=90 ) THEN check_count ELSE 0 END) AS check_count_90,
SUM(CASE WHEN (datediff(date(now()),date(modify_time)) <=180 ) THEN check_count ELSE 0 END) AS check_count_180,
COUNT(check_count>0 OR null) as check_num,-- 检查文件总数
COUNT((datediff(date(now()),date(modify_time)) <=30 ) and check_count>0 OR null) as check_num_30,
COUNT((datediff(date(now()),date(modify_time)) <=90 ) and check_count>0 OR null) as check_num_90,
COUNT((datediff(date(now()),date(modify_time)) <=180 ) and check_count>0 OR null) as check_num_180, 
CONCAT(ROUND(COUNT(check_count>0 OR null)/COUNT(1)*100,0),'%') as check_tage, -- 检查文件占比
CONCAT(ROUND(COUNT((datediff(date(now()),date(modify_time)) <=30 ) and check_count>0 OR null)/COUNT((datediff(date(now()),date(gmt_create)) <=30 ) OR null)*100,0),'%') as check_scale_30,
CONCAT(ROUND(COUNT((datediff(date(now()),date(modify_time)) <=90 ) and check_count>0 OR null)/COUNT((datediff(date(now()),date(gmt_create)) <=90 ) OR null)*100,0),'%') as check_scale_90,
CONCAT(ROUND(COUNT((datediff(date(now()),date(modify_time)) <=180 ) and check_count>0 OR null)/COUNT((datediff(date(now()),date(gmt_create)) <=180 ) OR null)*100,0),'%') as check_scale_180,
CONCAT(ROUND(COUNT(check_status=2 OR null)/COUNT(check_count>0 OR null)*100,0),'%') as pass_tage, -- 通过率
CONCAT(ROUND(COUNT((datediff(date(now()),date(modify_time)) <=30 ) and check_status=2 OR null)/COUNT((datediff(date(now()),date(modify_time)) <=30 ) and check_count>0 OR null)*100,0),'%') as pass_scale_30,
CONCAT(ROUND(COUNT((datediff(date(now()),date(modify_time)) <=90 ) and check_status=2 OR null)/COUNT((datediff(date(now()),date(modify_time)) <=90 ) and check_count>0 OR null)*100,0),'%') as pass_scale_90,
CONCAT(ROUND(COUNT((datediff(date(now()),date(modify_time)) <=180 ) and check_status=2 OR null)/COUNT((datediff(date(now()),date(modify_time)) <=180 ) and check_count>0 OR null)*100,0),'%') as pass_scale_180,
SUM(correct_count) as correct_count,-- 整改条数
SUM(correct_count_30) as correct_count_30,
SUM(correct_count_90) as correct_count_90,
SUM(correct_count_180) as correct_count_180,
SUM(out_time_count) as out_time_count, -- 逾期未完成整改条数
SUM(out_time_count_30) as out_time_count_30,
SUM(out_time_count_90) as out_time_count_90,
SUM(out_time_count_180) as out_time_count_180
from (

SELECT SUBSTRING_INDEX(d.bus_id,'_',1) as project_id,d.project_unit_id,d.node_id,ifnull(d.check_count,0) as check_count,d.check_status,ifnull(a.correct_count,0) as correct_count,ifnull(a.correct_count_30,0) as correct_count_30,ifnull(a.correct_count_90,0) as correct_count_90,ifnull(a.correct_count_180,0) as correct_count_180,ifnull(a.out_time_count,0) as out_time_count,ifnull(a.out_time_count_30,0) as out_time_count_30,ifnull(a.out_time_count_90,0) as out_time_count_90,ifnull(a.out_time_count_180,0) as out_time_count_180,u.unit_name,p.project_name,d.modify_time,d.gmt_create from v_doc_info d
INNER JOIN project_detail p on p.local_project_id=d.project_id
LEFT JOIN project_unit u on u.id=d.project_unit_id
left join( 
SELECT local_project_id,IFNULL(correct_soure_id,0)as node_id,COUNT(1) as correct_count
,COUNT(datediff(date(now()),date(create_time)) <=30) as correct_count_30
,COUNT(datediff(date(now()),date(create_time)) <=90) as correct_count_90
,COUNT(datediff(date(now()),date(create_time)) <=180) as correct_count_180
,count((have_deleted=0 and correct_end_time<NOW()) or null) as out_time_count
,count((have_deleted=0 and correct_end_time<NOW() and (datediff(date(now()),date(create_time)) <=30)) or null) as out_time_count_30
,count((have_deleted=0 and correct_end_time<NOW() and (datediff(date(now()),date(create_time)) <=90)) or null) as out_time_count_90
,count((have_deleted=0 and correct_end_time<NOW() and (datediff(date(now()),date(create_time)) <=180)) or null) as out_time_count_180  from doc_correct GROUP BY local_project_id,correct_soure_id
) a on a.node_id=d.node_id
UNION ALL
SELECT d.project_id,d.remote_project_id as project_unit_id,d.id as node_id,ifnull(d.check_count,0) as check_count,d.check_status,ifnull(a.correct_count,0) as correct_count,ifnull(a.correct_count_30,0) as correct_count_30,ifnull(a.correct_count_90,0) as correct_count_90,ifnull(a.correct_count_180,0) as correct_count_180,ifnull(a.out_time_count,0) as out_time_count,ifnull(a.out_time_count_30,0) as out_time_count_30,ifnull(a.out_time_count_90,0) as out_time_count_90,ifnull(a.out_time_count_180,0) as out_time_count_180,u.unit_name,p.project_name,d.modify_time,d.gmt_create from v_datumdata_info d
INNER JOIN project_detail p on p.local_project_id=d.project_id
LEFT JOIN project_unit u on u.id=d.remote_project_id
left join( 
SELECT local_project_id,IFNULL(correct_soure_id,0)as node_id,COUNT(1) as correct_count
,COUNT(datediff(date(now()),date(create_time)) <=30) as correct_count_30
,COUNT(datediff(date(now()),date(create_time)) <=90) as correct_count_90
,COUNT(datediff(date(now()),date(create_time)) <=180) as correct_count_180
,count((have_deleted=0 and correct_end_time<NOW()) or null) as out_time_count
,count((have_deleted=0 and correct_end_time<NOW() and (datediff(date(now()),date(create_time)) <=30)) or null) as out_time_count_30
,count((have_deleted=0 and correct_end_time<NOW() and (datediff(date(now()),date(create_time)) <=90)) or null) as out_time_count_90
,count((have_deleted=0 and correct_end_time<NOW() and (datediff(date(now()),date(create_time)) <=180)) or null) as out_time_count_180  from doc_correct GROUP BY local_project_id,correct_soure_id
) a on a.node_id=d.id
)t GROUP BY project_id,project_unit_id

第二步:创建预处理定时任务

    /**
     * 资料检查统计预加载定时任务
     */
    @PostConstruct //程序启动时就执行一次
    @Scheduled(cron = "${schedul.statistics.checkStatistics:0 0 * * * ?}") //默认定时频率每小时执行一次
    public void preProjectStatistics(){
        //0.删除临时表里的数据
        statisticsDao.deleteStatisticsTmpData();
        //1.将数据插入临时表
        statisticsDao.insertStatisticsTmpData();
        //2.删除正式表里的数据
        statisticsDao.deleteStatisticsData();
        //3.将临时表里的数据插入正式表
        statisticsDao.insertStatisticsData();
        //3.删除临时表里的数据
        statisticsDao.deleteStatisticsTmpData();
    }

对应的sql语句:

<insert id="insertStatisticsTmpData">
        insert into project_statistics_tmp SELECT * from v_project_statistics_info
    </insert>
    <delete id="deleteStatisticsData" parameterType="java.lang.Long">
        delete from project_statistics;
    </delete>
    <insert id="insertStatisticsData">
        insert into project_statistics
    select * from project_statistics_tmp;
    </insert>
    <delete id="deleteStatisticsTmpData" parameterType="java.lang.Long">
        delete from project_statistics_tmp;
    </delete>

第三步: 修改之前的统计sql从多表关联到查询单表速度有了质的提升