How do you call a function in mysql?

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 ?

How do you call a function in mysql?

asked Jan 16, 2013 at 10:53

1

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.

How do you call a function in mysql?

Blue

22.2k7 gold badges56 silver badges87 bronze badges

answered Mar 13, 2014 at 2:16

wedev27wedev27

2432 silver badges7 bronze badges

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.

answered Jan 16, 2013 at 11:02

triclosantriclosan

5,4466 gold badges25 silver badges49 bronze badges

1

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;

answered Jan 16, 2013 at 10:58

NajzeroNajzero

3,14417 silver badges18 bronze badges

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.

answered May 16, 2015 at 1:14

How do you call a function in mysql?

Sumit MunotSumit Munot

3,6601 gold badge29 silver badges51 bronze badges

2

How do you call a function in a database?

How to call an existing function in a database using JDBC API?.
Connect to the database..
Create a PreparedStatement object and to its constructor pass the function call in String format..
Set values to the place holders..
Execute the Callable statement..

How do you create a function and call in MySQL?

The syntax to create a function in MySQL is: CREATE FUNCTION function_name [ (parameter datatype [, parameter datatype]) ] RETURNS return_datatype BEGIN declaration_section executable_section END; function_name. The name to assign to this function in MySQL.

How do you execute a function in SQL?

How to execute user-defined function in SQL with parameters.
We create a function with the CREATE FUNCTION statement..
We give a name to the function..
We specify input parameters along with their data types..
We specify the data type of the value that the function will return..

How do you call a stored function in SQL?

How To Call A Function In SQL Server Stored procedure.
create function function_to_be_called(@username varchar(200)).
returns varchar(100).
begin..
declare @password varchar(200).
set @password=(select [password] from [User] where username =@username).
return @password..