2

in MySQL, given the following query:

select column1
, column2
, my_function1(column3) as f1
, my_function2(column4) as f2
, my_function3 (my_function1(column3), my_function2(column4)) as f3
where some condition on column 1 
having f1 > some value

does my_function1(column3) will be called 3 times? Or there's some optimization/cache that re-use the calculated value?

thank you

4

3 回答 3

0

The function will be called for each calculation separately. you can reuse it as many times a shout want within a single query.

于 2013-04-22T17:18:58.980 回答
0

Your function will be called 3 times unless declared as deterministic, but this depends of your MySQL version and i'm not pretty sure the function will be called once, if you want more info, read here: http://dev.mysql.com/doc/refman/5.6/en/create-procedure.html

于 2013-04-22T17:24:14.833 回答
0

Here's a short experiment to see what happens.

create function determin_rand (i integer) 
returns float DETERMINISTIC
return rand();

create function not_determin_rand (i integer) 
returns float 
return rand();

select determin_rand(1) as d1 , determin_rand(1) as d2, 
  not_determin_rand(1) as nd1, not_determin_rand(1) as nd2

0.00850549154   0.831901073456  0.133989050984  0.174242004752

Since the values are different, the function is getting called each time. In the first function I declared it deterministic, but it didn't make a difference.

I made a sqlfiddle for you to try it out with different versions of mysql.

http://sqlfiddle.com/#!2/a8536/2

于 2013-04-22T17:32:21.887 回答