Does MySQL store IP as an int to improve performance?

I have a database with a lot of queries per second. Search query for IP address value. So is it wise to store IP addresses, for example, 34.549.53.23as an int: 345495323 value? Will requests be faster?

I know, for example, 192.168.1.1and 192.16.81.1the two are stored as 19216811, but it does not matter.

+4
source share
2 answers

Although I believe this is micro-optimization, you can use the function ip2long()to convert the ascii representation of the IP address to a 32-bit integer and long2ip()to convert it.

Try:

$int = ip2long('192.168.0.1');
echo $int; // 3232235521

$ip = long2ip(3232235521);
echo $ip; // 192.168.0.1

Btw, IP , . , 192.168.0.1 19216801. , , . 19.216.80.1 192.168.0.1?

IP- - 32- , (, ). 3232235521 (192.168.0.1):

11000000 10101000 00000000 00000001

8 () . , . .

ascii .:

11000000 => 192
10101000 => 168
00000000 =>   0
00000001 =>   1

.. 192.168.0.1, . ( , )

+8

, . , IP , mysql INET_ATON() IP- INET_NTOA().

http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

PHP, ip2long() long2ip() viceversa

http://www.php.net/manual/en/function.ip2long.php

+5

Source: https://habr.com/ru/post/1540366/


All Articles