Mysql function call

前端 未结 4 877
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 04:34

If I call a function several time then will it execute every time or just execute once and the value will be used then after several time? Example:

 select my_fu         


        
相关标签:
4条回答
  • 2021-02-10 05:08

    Why not use a variable that catch the value of your function. For example:

    declare var_function (datatype(size)); // just to declare proper data type for your function
    
    set var_function = my_function('filed');
    
    select var_function, var_function/field2, 
            (var_function*field1)/field3,
    
    ....from my_table    where group by filed1;
    

    in that case, you'll be going to reuse the function result and no need to repeat the process of the function.

    0 讨论(0)
  • 2021-02-10 05:12

    It is possible to have some optimization if you declare your function as DETERMINISTIC. But it really should be deterministic:

    A routine is considered “deterministic” if it always produces the same result for the same input parameters, and “not deterministic” otherwise. If neither DETERMINISTIC nor NOT DETERMINISTIC is given in the routine definition, the default is NOT DETERMINISTIC. To declare that a function is deterministic, you must specify DETERMINISTIC explicitly.

    Assessment of the nature of a routine is based on the “honesty” of the creator: MySQL does not check that a routine declared DETERMINISTIC is free of statements that produce nondeterministic results. However, misdeclaring a routine might affect results or affect performance. Declaring a nondeterministic routine as DETERMINISTIC might lead to unexpected results by causing the optimizer to make incorrect execution plan choices. Declaring a deterministic routine as NONDETERMINISTIC might diminish performance by causing available optimizations not to be used. Prior to MySQL 5.0.44, the DETERMINISTIC characteristic is accepted, but not used by the optimizer.

    0 讨论(0)
  • 2021-02-10 05:20

    It's very simple to run the MySQL function.

    Login to MySQL command prompt using command:

    $> mysql -u root -p
    

    Then use the database using:

    mysql> use database_name
    

    Then run the MySQL function using:

    mysql> delimiter //
    
    mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
        -> BEGIN
        ->   SELECT COUNT(*) INTO param1 FROM t;
        -> END//
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> delimiter ;
    
    mysql> CALL simpleproc(@a);
    Query OK, 0 rows affected (0.00 sec)
    

    Instead of procedure we can add any multiple line function in above example.

    0 讨论(0)
  • 2021-02-10 05:24

    As far as I know (not a mysql pro) it is calling this function every time. Your expalin plan should show that issue.

    If you always call the function with the same argument, rather query it once per row via a sub-query.

    select funcvalue, funcvalue/field2, (funcvalue*field1)/field3,...... 
    from SELECT( my_function('filed') funcvalue, ... your other columns... 
    FROM TABLE )
    where group by filed1;
    
    0 讨论(0)
提交回复
热议问题