Slow nested function



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















Please let me know what information you seek to improve the question, rather than just downvoting.



I have a function that looks like this:



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 ;


The function for splitfactor is:



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 ;


The function f_splitadjprice runs extremely slow. About 14 seconds PR row. I have tried to run the individual pieces of the function by themselves. That is, the function call f_splitfactor and SELECT (f.p_price FROM fp_v2_fp_basic_prices as f WHERE f.fsym_id = id AND
f.p_date = startdate). When running these two by themselves outside of the function they take 0,001 seconds to run. So the whole problem is that as soon as I want to do in combination through the nested function it takes 100.000 times longer?










share|improve this question
























  • Can you add the code for f_splitfactor please.

    – P.Salmon
    Nov 15 '18 at 9:10











  • @P.Salmon Sure mate :)

    – Chris
    Nov 15 '18 at 9:11











  • @P.Salmon Can you help me?

    – Chris
    Nov 15 '18 at 11:55











  • @Chris Please read meta.stackoverflow.com/questions/333952/… on how to ask SQL related questions. Also provide the result of EXPLAIN <select statement> statements which give you a hint on how the query is being executed.

    – Progman
    Nov 15 '18 at 13:39

















-1















Please let me know what information you seek to improve the question, rather than just downvoting.



I have a function that looks like this:



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 ;


The function for splitfactor is:



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 ;


The function f_splitadjprice runs extremely slow. About 14 seconds PR row. I have tried to run the individual pieces of the function by themselves. That is, the function call f_splitfactor and SELECT (f.p_price FROM fp_v2_fp_basic_prices as f WHERE f.fsym_id = id AND
f.p_date = startdate). When running these two by themselves outside of the function they take 0,001 seconds to run. So the whole problem is that as soon as I want to do in combination through the nested function it takes 100.000 times longer?










share|improve this question
























  • Can you add the code for f_splitfactor please.

    – P.Salmon
    Nov 15 '18 at 9:10











  • @P.Salmon Sure mate :)

    – Chris
    Nov 15 '18 at 9:11











  • @P.Salmon Can you help me?

    – Chris
    Nov 15 '18 at 11:55











  • @Chris Please read meta.stackoverflow.com/questions/333952/… on how to ask SQL related questions. Also provide the result of EXPLAIN <select statement> statements which give you a hint on how the query is being executed.

    – Progman
    Nov 15 '18 at 13:39













-1












-1








-1








Please let me know what information you seek to improve the question, rather than just downvoting.



I have a function that looks like this:



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 ;


The function for splitfactor is:



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 ;


The function f_splitadjprice runs extremely slow. About 14 seconds PR row. I have tried to run the individual pieces of the function by themselves. That is, the function call f_splitfactor and SELECT (f.p_price FROM fp_v2_fp_basic_prices as f WHERE f.fsym_id = id AND
f.p_date = startdate). When running these two by themselves outside of the function they take 0,001 seconds to run. So the whole problem is that as soon as I want to do in combination through the nested function it takes 100.000 times longer?










share|improve this question
















Please let me know what information you seek to improve the question, rather than just downvoting.



I have a function that looks like this:



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 ;


The function for splitfactor is:



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 ;


The function f_splitadjprice runs extremely slow. About 14 seconds PR row. I have tried to run the individual pieces of the function by themselves. That is, the function call f_splitfactor and SELECT (f.p_price FROM fp_v2_fp_basic_prices as f WHERE f.fsym_id = id AND
f.p_date = startdate). When running these two by themselves outside of the function they take 0,001 seconds to run. So the whole problem is that as soon as I want to do in combination through the nested function it takes 100.000 times longer?







mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:26







Chris

















asked Nov 15 '18 at 9:00









ChrisChris

1189




1189












  • Can you add the code for f_splitfactor please.

    – P.Salmon
    Nov 15 '18 at 9:10











  • @P.Salmon Sure mate :)

    – Chris
    Nov 15 '18 at 9:11











  • @P.Salmon Can you help me?

    – Chris
    Nov 15 '18 at 11:55











  • @Chris Please read meta.stackoverflow.com/questions/333952/… on how to ask SQL related questions. Also provide the result of EXPLAIN <select statement> statements which give you a hint on how the query is being executed.

    – Progman
    Nov 15 '18 at 13:39

















  • Can you add the code for f_splitfactor please.

    – P.Salmon
    Nov 15 '18 at 9:10











  • @P.Salmon Sure mate :)

    – Chris
    Nov 15 '18 at 9:11











  • @P.Salmon Can you help me?

    – Chris
    Nov 15 '18 at 11:55











  • @Chris Please read meta.stackoverflow.com/questions/333952/… on how to ask SQL related questions. Also provide the result of EXPLAIN <select statement> statements which give you a hint on how the query is being executed.

    – Progman
    Nov 15 '18 at 13:39
















Can you add the code for f_splitfactor please.

– P.Salmon
Nov 15 '18 at 9:10





Can you add the code for f_splitfactor please.

– P.Salmon
Nov 15 '18 at 9:10













@P.Salmon Sure mate :)

– Chris
Nov 15 '18 at 9:11





@P.Salmon Sure mate :)

– Chris
Nov 15 '18 at 9:11













@P.Salmon Can you help me?

– Chris
Nov 15 '18 at 11:55





@P.Salmon Can you help me?

– Chris
Nov 15 '18 at 11:55













@Chris Please read meta.stackoverflow.com/questions/333952/… on how to ask SQL related questions. Also provide the result of EXPLAIN <select statement> statements which give you a hint on how the query is being executed.

– Progman
Nov 15 '18 at 13:39





@Chris Please read meta.stackoverflow.com/questions/333952/… on how to ask SQL related questions. Also provide the result of EXPLAIN <select statement> statements which give you a hint on how the query is being executed.

– Progman
Nov 15 '18 at 13:39












1 Answer
1






active

oldest

votes


















0














The solution is to not call tables within functions. In general that seems to be bad practice and it is nevertheless extremely slow. One should instead try to get rid of the function and perform the function directly in the query.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315693%2fslow-nested-function%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The solution is to not call tables within functions. In general that seems to be bad practice and it is nevertheless extremely slow. One should instead try to get rid of the function and perform the function directly in the query.






    share|improve this answer



























      0














      The solution is to not call tables within functions. In general that seems to be bad practice and it is nevertheless extremely slow. One should instead try to get rid of the function and perform the function directly in the query.






      share|improve this answer

























        0












        0








        0







        The solution is to not call tables within functions. In general that seems to be bad practice and it is nevertheless extremely slow. One should instead try to get rid of the function and perform the function directly in the query.






        share|improve this answer













        The solution is to not call tables within functions. In general that seems to be bad practice and it is nevertheless extremely slow. One should instead try to get rid of the function and perform the function directly in the query.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 16:22









        ChrisChris

        1189




        1189





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315693%2fslow-nested-function%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo