MYSQL存储过程运行得非常慢(连接超时)

时间:2015-07-20 18:09:45

标签: mysql mysql-workbench

BEGIN

declare currID INT(30);
declare maxID INT(30);
declare first_period datetime;
declare s_date datetime;
declare tot_sent INT(30);
declare weighted_score FLOAT(30);
declare aid INT(10);

set first_period = date_add(stats_start_from, INTERVAL 49 DAY);
set currID = (select min(rowid) from weekly_stats);
set maxID = (select max(rowid) from weekly_stats);

WHILE (currID <= 40000) DO

select accountid, week_start_date INTO aid, s_date from weekly_stats where rowid = currID;

if (s_date >= first_period) then

set tot_sent = (select sum(weekly_total_sent) from weekly_stats where (accountid = aid AND week_start_date <= s_date AND week_start_date >= (date_add(s_date, INTERVAL (-49) DAY)))); 
set weighted_score = (select sum(weekly_total_sent*weekly_raw_score) from weekly_stats where (accountid = aid AND week_start_date <= s_date AND week_start_date >= (date_add(s_date, INTERVAL (-49) DAY))));
set weighted_score = weighted_score/tot_sent;

update weekly_stats set weighted_scores = weighted_score where rowid =   currID;

end if;

set currID = currID + 1;

end while;

END

基本上,我在表格中有一个名为weighted_scores的列,计算如下:加权得分=过去8周(原始得分*已发送邮件)的总和除以过去8周内发送的总邮件数。因此,它是按照每周发送的邮件量加权的8周平均分。

我的程序运行得非常慢,并且即使是少量行也会超时(十分钟后)。我想知道问题是什么,可能是低效的查询,计算等?任何想法将不胜感激。谢谢!

create table weekly_stats
(
    accountid INT(10),
    week_start_date datetime,
    weekly_total_sent INT(30),
    weekly_hbounce INT(30),
    weekly_sbounce INT(30),
    weekly_spam INT(30),
    weekly_optouts INT(30),
    weekly_opens INT(30),
    weekly_CT INT(30),
    rowid INT(30),
    weekly_raw_score FLOAT(30),
    weighted_scoresFLOAT(30)

    -- indexes not shown
);

0 个答案:

没有答案
相关问题