定义了一个存储过程,该存储过程带有一些参数,并在其中运行循环。问题是,每当我执行此查询时,此单个进程/函数都会占用100%的CPU使用率,大约需要2分钟才能执行。我增加了内核数量,但仍然占用了100%的CPU使用率。我在想,也许它正在创建临时表,而Postgresql无法自动清理。
我尝试了几种配置,但无济于事。
create
or replace
function some_function(offers numeric[],
list character varying[],
startdate date,
enddate date) returns table
(prod character varying,
offer numeric,
dates date,
baseline double precision,
promoincremental double precision,
couponincremental double precision,
affinityquantity double precision,
affinitymargin double precision,
affinityrevenue double precision,
cannibalisationquantity double precision,
cannibalisationrevenue double precision,
cannibalisationmargin double precision
) language plpgsql as $function$ declare a numeric[] := offers;
i numeric;
begin
foreach i in array a loop return QUERY select
models.prod,
i as offer,
date(models.transdate_dt) as dates,
greatest(0,(sum(models.unitretailprice) * sum(coefficients.unit_retail_price)) + (sum(models.flag::int) * sum(coefficients.flag::int)) + (sum(models.mc_baseline) * sum(coefficients.mc_baseline)) + (sum(models.mc_day_avg) * sum(coefficients.mc_day_avg)) + (sum(models.mc_day_normal) * sum(coefficients.mc_day_normal)) + (sum(models.mc_week_avg) * sum(coefficients.mc_week_avg)) + (sum(models.mc_week_normal) * sum(coefficients.mc_week_normal)) + (sum(models.sku_day_avg) * sum(coefficients.sku_day_avg)) + (sum(models.sku_month_avg) * sum(coefficients.sku_month_avg)) + (sum(models.sku_month_normal)* sum(coefficients.sku_month_normal)) + (sum(models.sku_moving_avg) * sum(coefficients.sku_moving_avg)) + (sum(models.sku_week_avg) * sum(coefficients.sku_week_avg)) + (sum(models.sku_week_normal)* sum(coefficients.sku_week_normal))) as baseline,
greatest(0, ((0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a)))) as promoIncremental,
greatest(0, (sum(models.basket_dollar_off) * sum(coefficients.basket_dollar_off)) + (sum(models.basket_per_off) * sum(coefficients.basket_per_off)) + (sum(models.category_dollar_off) * sum(coefficients.category_dollar_off)) + (sum(models.category_per_off) * sum(coefficients.category_per_off)) + (sum(models.disc_per) * sum(coefficients.disc_per))) as couponIncremnetal,
greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a))) * sum(affinity.pull) * sum(affinity.confidence) as affinityquantity,
greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a))) * sum(affinity.margin_lift) * sum(affinity.confidence) as affinitymargin,
greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a))) * sum(affinity.revenue_lift) * sum(affinity.confidence) as affinityrevenue,
greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a)))/(NULLIF( greatest(0,(sum(models.unitretailprice) * sum(coefficients.unit_retail_price)) + (sum(models.flag::int) * sum(coefficients.flag::int)) + (sum(models.mc_baseline) * sum(coefficients.mc_baseline)) + (sum(models.mc_day_avg) * sum(coefficients.mc_day_avg)) + (sum(models.mc_day_normal) * sum(coefficients.mc_day_normal)) + (sum(models.mc_week_avg) * sum(coefficients.mc_week_avg)) + (sum(models.mc_week_normal) * sum(coefficients.mc_week_normal)) + (sum(models.sku_day_avg) * sum(coefficients.sku_day_avg)) + (sum(models.sku_month_avg) * sum(coefficients.sku_month_avg)) + (sum(models.sku_month_normal)* sum(coefficients.sku_month_normal)) + (sum(models.sku_moving_avg) * sum(coefficients.sku_moving_avg)) + (sum(models.sku_week_avg) * sum(coefficients.sku_week_avg)) + (sum(models.sku_week_normal)* sum(coefficients.sku_week_normal))) + greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a))) + greatest(0, (sum(models.basket_dollar_off) * sum(coefficients.basket_dollar_off)) + (sum(models.basket_per_off) * sum(coefficients.basket_per_off)) + (sum(models.category_dollar_off) * sum(coefficients.category_dollar_off)) + (sum(models.category_per_off) * sum(coefficients.category_per_off)) + (sum(models.disc_per) * sum(coefficients.disc_per))), 0)) * sum(cannibalisation.effect) * (sum(products.price) - (sum(products.price) - i )) as cannibalisationquantity,
((sum(products.price) - i) * sum(models.disc_per) - (sum(products.price) - i) ) * ( greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a)))/(NULLIF( greatest(0,(sum(models.unitretailprice) * sum(coefficients.unit_retail_price)) + (sum(models.flag::int) * sum(coefficients.flag::int)) + (sum(models.mc_baseline) * sum(coefficients.mc_baseline)) + (sum(models.mc_day_avg) * sum(coefficients.mc_day_avg)) + (sum(models.mc_day_normal) * sum(coefficients.mc_day_normal)) + (sum(models.mc_week_avg) * sum(coefficients.mc_week_avg)) + (sum(models.mc_week_normal) * sum(coefficients.mc_week_normal)) + (sum(models.sku_day_avg) * sum(coefficients.sku_day_avg)) + (sum(models.sku_month_avg) * sum(coefficients.sku_month_avg)) + (sum(models.sku_month_normal)* sum(coefficients.sku_month_normal)) + (sum(models.sku_moving_avg) * sum(coefficients.sku_moving_avg)) + (sum(models.sku_week_avg) * sum(coefficients.sku_week_avg)) + (sum(models.sku_week_normal)* sum(coefficients.sku_week_normal))) + greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a))) + greatest(0, (sum(models.basket_dollar_off) * sum(coefficients.basket_dollar_off)) + (sum(models.basket_per_off) * sum(coefficients.basket_per_off)) + (sum(models.category_dollar_off) * sum(coefficients.category_dollar_off)) + (sum(models.category_per_off) * sum(coefficients.category_per_off)) + (sum(models.disc_per) * sum(coefficients.disc_per))), 0)) * sum(cannibalisation.effect) * (sum(products.price) - (sum(products.price) - i ) )) as cannibalisationrevenue,
(((sum(products.price) - i) * sum(models.disc_per) - (sum(products.price) - i) ) - sum(products.cost)) * ( greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a)))/(NULLIF(greatest(0,(sum(models.unitretailprice) * sum(coefficients.unit_retail_price)) + (sum(models.flag::int) * sum(coefficients.flag::int)) + (sum(models.mc_baseline) * sum(coefficients.mc_baseline)) + (sum(models.mc_day_avg) * sum(coefficients.mc_day_avg)) + (sum(models.mc_day_normal) * sum(coefficients.mc_day_normal)) + (sum(models.mc_week_avg) * sum(coefficients.mc_week_avg)) + (sum(models.mc_week_normal) * sum(coefficients.mc_week_normal)) + (sum(models.sku_day_avg) * sum(coefficients.sku_day_avg)) + (sum(models.sku_month_avg) * sum(coefficients.sku_month_avg)) + (sum(models.sku_month_normal)* sum(coefficients.sku_month_normal)) + (sum(models.sku_moving_avg) * sum(coefficients.sku_moving_avg)) + (sum(models.sku_week_avg) * sum(coefficients.sku_week_avg)) + (sum(models.sku_week_normal)* sum(coefficients.sku_week_normal))) + greatest(0, (0 * sum(coefficients.f)) + (0 * sum(coefficients.p)) + (i * sum(coefficients.a))) + greatest(0, (sum(models.basket_dollar_off) * sum(coefficients.basket_dollar_off)) + (sum(models.basket_per_off) * sum(coefficients.basket_per_off)) + (sum(models.category_dollar_off) * sum(coefficients.category_dollar_off)) + (sum(models.category_per_off) * sum(coefficients.category_per_off)) + (sum(models.disc_per) * sum(coefficients.disc_per))), 0)) * sum(cannibalisation.effect) * (sum(products.price) - (sum(products.price) - i ) )) as cannibalisationmargin
from
models
join coefficients on
models.prod = coefficients.prod
and models.si_type = coefficients.si_type
and models.model_type = coefficients.model_type
left join products on
products.unique_id1 = models.prod
left join affinity on
affinity.prod = models.prod
left join cannibalisation on
cannibalisation.prod = models.prod
where
coefficients.prod = any(skusList)
and models.transdate_dt >= startDate
and models.transdate_dt <= endDate
group by
models.prod,
dates,
offer ;
end loop;
end;
$function$ ;
在这里,我使用以下参数调用函数:::::
select
*
from
some_function(array[1.5,
2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5 ],
'{12841276, 11873916,07915473, 01504273,10405843,11231446,12224242,11249380,08604365, 11248952, 11230018,10447621,11229820,10406916,09578733,11280161, 01503697, 11923554, 10406460,11295219,01458421,09053893,11224409, 06755789, 11317377, 11275047,12231817,11309507,10447522,10406296, 10406338, 01460658, 12272811,11318870,11248838,10406130,11248812, 11223682, 11276748, 10447605, 11232451, 10405827,10447670,08177743, 10405231, 02326791, 12224226,12231650,11929197,01504380 }',
'2018-01-02'::date,
'2018-01-06'::date );
我希望它最多可以占用大约30%的CPU使用率,因为它正在执行一些计算,但是却减少了100%的CPU使用率,有人可以在这方面帮忙吗?