Why is my custom MySQL function so much slower than inlining same in query? Why is my custom MySQL function so much slower than inlining same in query? mysql mysql

Why is my custom MySQL function so much slower than inlining same in query?


Don't reinvent the wheel, use INET_NTOA():

mysql> SELECT INET_NTOA(167773449);    -> '10.0.5.9'


Using this one you could get better performance:

CREATE FUNCTION IntToIp2(value INT UNSIGNED)  RETURNS char(15)  DETERMINISTIC  RETURN CONCAT_WS(    '.',     (value >> 24),    (value >> 16) & 255,    (value >>  8) & 255,     value        & 255  );> SELECT IntToIp(ip) FROM ips;1221202 rows in set (18.52 sec)> SELECT IntToIp2(ip) FROM ips;1221202 rows in set (10.21 sec)

Launching your original SELECT just after adding your test data took 4.78 secs on my system (2gB mysql 5.1 instance on quad core (fedora 64 bit).

EDIT: Is this much slowness expected?

Yes, stored procedures are slow, a bunch of magnitudes slower than interpreted/compiled code. They turn out useful when you need to tie up some database logic which you want to keep out of your application because it's out of the specific domain (ie, logging/administrative tasks). If a stored function contains no queries, it's always better practice to write an utility function in your chosen language, as that wont prevent reuse (there are no queries), and will run much faster.

And that's the reason for which, in this particular case, you should use the INET_NTOA function instead, which is available and fulfils your needs, as suggested in sanmai answer.