mysql中将ip转换为数值的函数

来源:互联网 发布:微店和淘宝哪个安全 编辑:程序博客网 时间:2024/05/01 17:20

mysql> help inet_aton

Name: ‘INET_ATON’ Description: Syntax: INET_ATON(expr)

Given the dotted-quad representation of an IPv4 network address as a string, returns an integer that represents the numeric value of the address in network byte order (big endian). INET_ATON() returns NULL if it does not understand its argument.

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

Examples:

mysql> SELECT INET_ATON(’10.0.5.9′);

-> 167773449

mysql> select inet_aton(’10.12.12.1′);

+————————-+

| inet_aton(’10.12.12.1′) |

+————————-+

|               168561665 |

+————————-+

1 row in set (0.00 sec)

转换为unsigned int只需4个字节,比varchar(15)类型存储好,而且有查询优势,特别是between ip1 and ip2的查询。

固定长度的表会提高性能,因为MySQL搜寻得会更快一些,因为这些固定的长度是很容易计算下一个数据的偏移量的,所以读取的自然也会很快。而如果字段不是定长的,那么,每一次要找下一条的话,需要程序找到主键。固定长度的表也更容易被缓存和重建。

0 0