面试官:如果要存 IP 地址,用什么数据类型比较好?很多人都会答错

56509c9301f98367b8f63ce75d3bd53b.jpeg

在看高性能MySQL第3版(4.1.7节)时,作者建议当存储IPv4地址时,应该使用32位的无符号整数(UNSIGNED INT)来存储IP地址,而不是使用字符串。 但是没有给出具体原因。为了搞清楚这个原因,查了一些资料,记录下来。

相对字符串存储,使用无符号整数来存储有如下的好处:

  • 节省空间,不管是数据存储空间,还是索引存储空间
  • 便于使用范围查询(BETWEEN...AND),且效率更高

通常,在保存IPv4地址时,一个IPv4最小需要7个字符,最大需要15个字符,所以,使用VARCHAR(15)即可。MySQL在保存变长的字符串时,还需要额外的一个字节来保存此字符串的长度。而如果使用无符号整数来存储,只需要4个字节即可。

另外还可以使用4个字段分别存储IPv4中的各部分,但是通常这不管是存储空间和查询效率应该都不是很高(可能有的场景适合使用这种方式存储)。

使用字符串和无符号整数来存储IP的具体性能分析及benchmark,可以看这篇文章。

https://bafford.com/2009/03/09/mysql-performance-benefits-of-storing-integer-ip-addresses/

使用无符号整数来存储也有缺点:

  • 不便于阅读
  • 需要手动转换

对于转换来说,MySQL提供了相应的函数来把字符串格式的IP转换成整数INET_ATON,以及把整数格式的IP转换成字符串的INET_NTOA。如下所示:

mysql>  select  inet_aton( '192.168.0.1');
+ --------------------------+
| inet_aton('192.168.0.1') |
+ --------------------------+
|               3232235521 |
+ --------------------------+
1 row in  set ( 0.00 sec)

mysql>  select  inet_ntoa( 3232235521);
+ -----------------------+
| inet_ntoa(3232235521) |
+ -----------------------+
| 192.168.0.1           |
+ -----------------------+
1 row in  set ( 0.00 sec)

对于IPv6来说,使用VARBINARY同样可获得相同的好处,同时MySQL也提供了相应的转换函数,即INET6_ATON和INET6_NTOA。

对于转换字符串IPv4和数值类型,可以放在应用层,下面是使用java代码来对二者转换:

package com.mikan;

/**
 * @author Mikan
 */

public  class IpLongUtils {
     /**
     * 把字符串IP转换成long
     *
     * @param ipStr 字符串IP
     * @return IP对应的long值
     */

     public static long ip2Long(String ipStr) {
        String[] ip = ipStr.split( "\\.");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp;(Long.valueOf(ip[ 0])&nbsp;<<&nbsp; 24)&nbsp;+&nbsp;(Long.valueOf(ip[ 1])&nbsp;<<&nbsp; 16)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;(Long.valueOf(ip[ 2])&nbsp;<<&nbsp; 8)&nbsp;+&nbsp;Long.valueOf(ip[ 3]);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;把IP的long值转换成字符串
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;ipLong&nbsp;IP的long值
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;long值对应的字符串
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/

&nbsp;&nbsp;&nbsp;&nbsp; public&nbsp;static&nbsp;String&nbsp;long2Ip(long&nbsp;ipLong)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuilder&nbsp;ip&nbsp;=&nbsp; new&nbsp;StringBuilder();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip.append(ipLong&nbsp;>>>&nbsp; 24).append( ".");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip.append((ipLong&nbsp;>>>&nbsp; 16)&nbsp;&&nbsp; 0xFF).append( ".");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip.append((ipLong&nbsp;>>>&nbsp; 8)&nbsp;&&nbsp; 0xFF).append( ".");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip.append(ipLong&nbsp;&&nbsp; 0xFF);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp;ip.toString();
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp; public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(ip2Long( "192.168.0.1"));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(long2Ip( 3232235521L));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(ip2Long( "10.0.0.1"));
&nbsp;&nbsp;&nbsp;&nbsp;}

}

输出结果为:

3232235521
192.168 .0 .1
167772161
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值