E
E
Eugene Ordinary2021-07-31 15:51:55
MySQL
Eugene Ordinary, 2021-07-31 15:51:55

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

2 answer(s)
S
Slava Rozhnev, 2021-07-31
@evgeniy8086

SELECT COUNT(*) FROM mytable 
WHERE myfun( field ) IN ('str1', 'str2', 'str3');

but it's still bad. It's better to find an inverse function and use
SELECT COUNT(*) FROM mytable 
WHERE field IN (myrevfunc('str1'), myrevfunc('str2'), myrevfunc('str2'));

L
Lazy @BojackHorseman MySQL, 2021-07-31
Tag

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 question

Ask a Question

731 491 924 answers to any question