I encountered a situation in which I have to call a function several times inside a view. something like:
select a,b,c, GetRefund(a) AS REFUND, d-GetRefund(a) AS Payment_left FROM bla bla bla.
Obviously I can’t join the refunds table to this view (or else I’d do it).
It seems the explain plan doesn’t consider the function’s cause no matter how many times I use them, explain looks the same.
What I’m asking is – does MYSQL optimize this and actually call the function a single time?
is there a better solution for doing this?
Thank you very much!
No. MySQL calls this function twice for each selected row
Is there a better solution? We’d need to know a bit more about your database structure, but something like this should reduce it to one function call per row: