Answer the question
In order to leave comments, you need to log in
Is calling a function multiple times in a MySQL query optimized?
In this query, will the myfun() user-defined function be evaluated three times for each field value, or will there be some kind of optimization? What can be done to optimize such a query?
SELECT COUNT(*) FROM mytable
WHERE myfun( field )='str1' OR myfun( field )='str2' OR myfun( field )='str3';
Answer the question
In order to leave comments, you need to log in
SELECT COUNT(*) FROM mytable
WHERE myfun( field ) IN ('str1', 'str2', 'str3');
SELECT COUNT(*) FROM mytable
WHERE field IN (myrevfunc('str1'), myrevfunc('str2'), myrevfunc('str2'));
will be calculated. and everything will be past the indexes.
index over generated column
with some restrictions on the implementation of myfun ()
if it cannot be expressed through allowed operators / literals / function calls, then you have to pervert triggers to calculate the values of the indexed field.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question