Mysql function call
Asked Answered
M

4

6

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_function('filed'),my_function('filed')/field2, 
        (my_function('filed')*field1)/field3,
...... from my_table    where group by filed1;

My question is my_function('filed') will be executed once and then the result will be used in my_function('filed')/field2 and (my_function('filed')*field1)/field3 or every time my_function('filed') will be called and executed in system level ?

Marcusmarcy answered 16/1, 2013 at 10:53 Comment(1)
the function is executed every time. Whether or not the value that comes back is cached depends on the implementation.Combination
R
8

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.

Runway answered 13/3, 2014 at 2:16 Comment(0)
H
3

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.

Hoban answered 16/1, 2013 at 11:2 Comment(1)
Thank you very much for your valuable information. I need to clarify one thing that is in above example , you will see that I have used group by clause and in reality I am calculating 84 fields using 12 stored function that is effecting on 511 rows. In this situation should i declare as DETERMINISTIC explicitly?Marcusmarcy
B
2

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;
Barred answered 16/1, 2013 at 10:58 Comment(0)
P
1

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.

Photolithography answered 16/5, 2015 at 1:14 Comment(2)
this doesn't really answer the question. Besides, CALL is only for procedures, not functionsVictimize
@Sebastianb, under the "delimiter //" prompt we can call functions as well. using CALL is just an example for reference of procedures.Photolithography

© 2022 - 2024 — McMap. All rights reserved.