I saw someone says that you can store IPV4 (4bytes) and IPV6 (16bytes) in a column of varbinary(16), and do something like the following code when you want to insert an IP:
INSERT INTO `log_table` (`ip_address`, `created_at`) VALUES(INET6_ATON(?), NOW());
The “?” is the IP to save.
I have no questions with the above code as it is self-explanatory.
But then he says that when you want to search an IP, do this:
SELECT INET6_NTOA(`ip_address`) AS `ip` FROM `log_table` WHERE `ip_address` = INET6_ATON(?);
I don’t understand the reason of selecting
INET6_NTOA(`ip_address`) rather than simply selecting
It does not seem to me that it is because searching with INET6_NTON() is more efficient in this case. I did a benchmark test and found that there is no noticeable difference of speed between selecting
INET6_NTOA(`ip_address`) and simply selecting
Maybe the person who said that misunderstood something?