Mysql函数调用

Sam*_*iul 5 mysql stored-functions

如果我多次调用一个函数,那么每次执行它还是只执行一次,然后在几次之后使用该值?例:

 select my_function('filed'),my_function('filed')/field2, 
        (my_function('filed')*field1)/field3,
...... from my_table    where group by filed1;
Run Code Online (Sandbox Code Playgroud)

我的问题 my_function('filed')将被执行一次然后结果将被用于my_function('filed')/field2和/ (my_function('filed')*field1)/field3或每次都my_function('filed')将在系统级别调用和执行?

wed*_*v27 7

为什么不使用捕获函数值的变量.例如:

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;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将重用函数结果,无需重复函数的过程.


Naj*_*ero 2

据我所知(不是mysql专业人士)它每次都会调用这个函数。你的解释计划应该显示这个问题。

如果您始终使用相同的参数调用该函数,请通过子查询每行查询一次。

select funcvalue, funcvalue/field2, (funcvalue*field1)/field3,...... 
from SELECT( my_function('filed') funcvalue, ... your other columns... 
FROM TABLE )
where group by filed1;
Run Code Online (Sandbox Code Playgroud)