我有一个看起来像这样的函数:
DELIMITER $$
DROP FUNCTION IF EXISTS f_splitadjprice;
CREATE FUNCTION f_splitadjprice (id CHAR(8), startdate DATE)
RETURNS FLOAT
BEGIN
DECLARE splitfactor FLOAT;
DECLARE splitadjprice FLOAT;
SELECT f_splitfactor(id, startdate) INTO splitfactor;
SELECT (f.p_price FROM fp_v2_fp_basic_prices as f WHERE f.fsym_id = id AND
f.p_date = startdate) * splitfactor INTO splitadjprice;
RETURN splitadjprice;
END$$
DELIMITER ;
splitfactor的功能是:
DELIMITER $$
DROP FUNCTION IF EXISTS f_splitfactor;
CREATE FUNCTION f_splitfactor (id CHAR(8), startdate DATE)
RETURNS FLOAT
BEGIN
DECLARE splitfactor FLOAT;
SELECT IFNULL(EXP(SUM(LOG(f.p_split_factor))),1) INTO splitfactor
FROM fp_v2_fp_basic_splits AS f
WHERE f.fsym_id = id AND f.p_split_date > startdate AND f.p_split_date <
NOW();
RETURN splitfactor;
END$$
DELIMiTER ;
函数f_splitadjprice运行非常慢。 PR排大约14秒。我尝试自己运行该功能的各个部分。也就是说,函数调用f_splitfactor和SELECT(f.p_price FROM fp_v2_fp_basic_prices as f W.f.fsym_id = id AND f.p_date =开始日期)。在功能之外单独运行这两个函数时,它们需要运行0.001秒。因此,整个问题是,只要我想通过嵌套函数进行组合,就需要100.000倍的时间?
答案 0 :(得分:0)
解决方案是不调用函数内的表。通常,这似乎是不好的做法,但是它却非常缓慢。相反,应该尝试摆脱该功能并直接在查询中执行该功能。