Store and index IP Address in MySQL table - Faster!
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!
