使用过程根据列中的状态对大数据进行计数

问题描述

表中有770万条记录(并且正在迅速增长),计算所有记录最多需要19秒。

我的要求是在1秒钟内加载它,请帮助我....

我的表结构是->

id  |campaign_id  | disposition |   action    create_date | more 16 columns...
1   |     3       |   CANCEL    |     NULL    2020-08-12  |
2   |     5       |   ANSWER    |     DNC     2020-08-13  |
3   |     3       |   ANSWER    |     SIP     2020-08-14  |

我的程序是->

proc_campaign_report(campaignId int(10),reportTable varchar(20))
BEGIN
 SET @SELECTFROM = CONCAT("
        select  create_date as dialDate,count(id) as calls,sum(duration) as duration,sum(client_cost) as clientCost,sum(vendor_cost) as vendorCost,count(case when action = 'DNC' then 1 end) as dontCall,count(case when action = 'AM' then 1 end) as am,count(case when action in ('SIP','Hangup','Phone') then 1 end) as transfer,count(case when disposition = 'ANSWER' then 1 end
                     ) as answer,count(case when disposition = 'CONGESION' then 1 end) as congesion,count(case when disposition = 'BUSY' then 1 end) as busy,count(case when disposition = 'CANCEL' then 1 end) as cancel,count(case when disposition = 'NO ANSWER' then 1 end) as noAnswer,count(case when disposition is NULL then 1 end) as other,count(case when disposition = 'FAILED' then 1 end) as failed
            from  ",reportTable 
                         );
SET @SELECTWHERE = CONCAT('
            where  `campaign_id` =',campaignId 
                         );
SET @SELECTQUERY = CONCAT(@SELECTFROM,@SELECTWHERE 
                         );
SET @QUERY = CONCAT(@SELECTQUERY,'
            group by  `dialDate`
            order by  `dialDate` desc
            limit  3'
                   );
PREPARE stmt
    FROM  @QUERY;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END 

请建议我是否有任何解决方法.....

我也尝试过索引编制,但是它给我的结果不正确...

解决方法

从查找该campaignId的第三个最新的DialDate开始。然后添加到WHERE子句AND dialDate >= @third

也有这个综合索引:INDEX(campaignId,dialDate)

如果这还不够帮助,请提供

  • 生成的查询。 (跟随紧随者我可能很棘手。)
  • EXPLAIN SELECT ...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...