码迷,mamicode.com
首页 > 数据库 > 详细

MySQL怎样存储IP地址

时间:2015-05-11 13:11:00      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

为什么要问如何存储IP

首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗?

其实做任何程序设计都要在功能实现的基础上最大限度的优化性能。而数据库设计是程序设计中不可忽略的一个重要部分,所以巧存IP地址可以一定程度获得很大提升。

 

利用函数算法处理

在MySQL中没有直接提供IP类型字段,但如果有两个函数可以把IP与最大长度为10位数字类型互转,所以使用int类型存储IP比varchar类型存储IP地址性能要提升很多,减少不少空间。因为varchar是可变长形,需要多余的一个字节存储长度。另外int型在逻辑运算上要比varchar速度快。

 

IP转数字函数inet_aton()

我们转换下几个常用的IP地址

mysql> select inet_aton(‘255.255.255.255‘);
+------------------------------+
| inet_aton(‘255.255.255.255‘) |
+------------------------------+
|                   4294967295 |
+------------------------------+
1 row in set (0.00 sec)

mysql> select inet_aton(‘192.168.1.1‘);    
+--------------------------+
| inet_aton(‘192.168.1.1‘) |
+--------------------------+
|               3232235777 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select inet_aton(‘10.10.10.10‘);
+--------------------------+
| inet_aton(‘10.10.10.10‘) |
+--------------------------+
|                168430090 |
+--------------------------+
1 row in set (0.00 sec)

所以IP的表字段可以设置为INT(10)就好,如果IP获取不到可以直接存0代表获取不到IP的意思

 

数字转IP函数inet_ntoa()

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

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

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

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

注意,0转换为 0.0.0.0

 

整型字段的比较比字符串效率高很多,这也符合一项优化原则:字段类型定义使用最合适(最小),最简单的数据类型。
inet_aton()算法,其实借用了国际上对各国IP地址的区分中使用的ip number。
a.b.c.d 的ip number是:
a * 256的3次方 + b * 256的2次方 + c * 256的1次方 + d * 256的0次方。

 

来源:
http://www.qttc.net/201208193.html
http://blog.sina.com.cn/s/blog_499740cb0100giny.html

/**
 * @author SunChong
 */
public class IpUtil {

    /**
     * 将字符串型ip转成int型ip
     * @param strIp
     * @return
     */
    public static int Ip2Int(String strIp){
        String[] ss = strIp.split("\\.");
        if(ss.length != 4){
            return 0;
        }
        byte[] bytes = new byte[ss.length];
        for(int i = 0; i < bytes.length; i++){
            bytes[i] = (byte) Integer.parseInt(ss[i]);
        }
        return byte2Int(bytes);
    }
    /**
     * 将int型ip转成String型ip
     * @param intIp
     * @return
     */
    public static String int2Ip(int intIp){
        byte[] bytes = int2byte(intIp);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < 4; i++){
            sb.append(bytes[i] & 0xFF);
            if(i < 3){
                sb.append(".");
            }
        }
        return sb.toString();
    }

    private static byte[] int2byte(int i) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (0xff & i);
        bytes[1] = (byte) ((0xff00 & i) >> 8);
        bytes[2] = (byte) ((0xff0000 & i) >> 16);
        bytes[3] = (byte) ((0xff000000 & i) >> 24);
        return bytes;
    }
    private static int byte2Int(byte[] bytes) {
        int n = bytes[0] & 0xFF;
        n |= ((bytes[1] << 8) & 0xFF00);
        n |= ((bytes[2] << 16) & 0xFF0000);
        n |= ((bytes[3] << 24) & 0xFF000000);
        return n;
    }

    public static void main(String[] args) {
        String ip1 = "192.168.0.1";
        int intIp = Ip2Int(ip1);
        String ip2 = int2Ip(intIp);
        System.out.println(ip2.equals(ip1));
    }
}


MySQL怎样存储IP地址

标签:

原文地址:http://my.oschina.net/ydsakyclguozi/blog/413208

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!