使用过程根据列中的状态计算大数据
Count large data based on status in the columns using procedure
table 中有 770 万条记录(并且增长迅速),统计所有记录最多需要 19 秒。
我的要求是在 1 秒内加载它,请帮助我....
我的 table 结构是 ->
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 ...
table 中有 770 万条记录(并且增长迅速),统计所有记录最多需要 19 秒。
我的要求是在 1 秒内加载它,请帮助我....
我的 table 结构是 ->
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 ...