Store and index IP Address in MySQL table - Faster!

E-mail
Written by Henrik, Monday, 19 January 2009 Last Updated ( Tuesday, 24 February 2009 20:03 )

Normally one would store an IP Address in something like a VARCHAR(15). But when you have a table with a large amount of data and you need to index this field there are a better and faster option!

As many other nice built in things in MySQL there are two functions, INET_ATON() and INET_NTOA(). These two functions are based on the two functions with the same names that are in any tcp capable systems C library.

INET_ATON() will convert an IP Address to an unsigned 32-bit integer, and INET_NTOA() does the opposite thing.

Example;

mysql> SELECT INET_ATON('192.168.0.10') AS ipn;
+------------+
| ipn |
+------------+
| 3232235530 |
+------------+

mysql> SELECT INET_NTOA(3232235530) AS ipa;
+--------------+
| ipa |
+--------------+
| 192.168.0.10 |
+--------------+

Thanks to Arjen's Journal for the tip!

Like this Joomla template?

As many other things on this site it is completely free! In collaboration with Midsjö AB are we releasing it into the public domain under the GNU/GPL version 2 license. You will find this template and other extensions under the Labs section then Joomla!. Good luck and happy Joomling!