Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Mysql cache calls to same function with same arguments

For example I have this condition

WHERE f1=CONCAT(v1,v2) OR f2=CONCAT(v1,v2) -- /*...

where v1,v1 are static, then Mysql must cache result of concat after first call. If v1 is field, then Mysql must cache result of concat after first call, but only for current row.

So, Mysql doing this?

like image 361
Nibik Alexander Avatar asked Apr 26 '26 05:04

Nibik Alexander


1 Answers

No, MySQL does not cache function calls.

Furthermore, such an optimization would not be worth doing. Note the tiny difference:

mysql> SELECT city, country, CONCAT(city, country) FROM cities LIMIT 263000,5
+----------+---------+-----------------------+
| city     | country | CONCAT(city, country) |
+----------+---------+-----------------------+
| Kitanzi  | cg      | Kitanzicg             |
| Masend   | cg      | Masendcg              |
| Chilute  | ao      | Chiluteao             |
| Khilule  | ao      | Khiluleao             |
| Tchibuti | ao      | Tchibutiao            |
+----------+---------+-----------------------+
5 rows in set (0.10 sec)

mysql> SELECT city, country FROM cities LIMIT 263000, 5;
+----------+---------+
| city     | country |
+----------+---------+
| Kitanzi  | cg      |
| Masend   | cg      |
| Chilute  | ao      |
| Khilule  | ao      |
| Tchibuti | ao      |
+----------+---------+
5 rows in set (0.09 sec)

Fetching the rows is more costly than any function calls in the expressions.

You can, however, use @variables to do the caching yourself. Again, you won't gain much, if any, speed. (However, you might gain simplicity in your code.)

like image 118
Rick James Avatar answered Apr 27 '26 19:04

Rick James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!