当前位置:网站首页>If you want to save an IP address, what data type is better? 99% of people will answer wrong!

If you want to save an IP address, what data type is better? 99% of people will answer wrong!

2022-06-11 07:18:00 Java notes shrimp

Click on the official account , utilize Fragment time to learn

Looking at high performance MySQL The first 3 edition (4.1.7 section ) when , Author's suggestion When storing IPv4 Address time , You should use 32 An unsigned integer of bits (UNSIGNED INT) To store IP Address , Instead of using strings .  But no specific reasons were given . To find out why , I checked some information , recorded .

Relative string storage , Using unsigned integers for storage has the following benefits :

  • Save a space , Whether it's data storage space , Or index storage space

  • Easy to use range query (BETWEEN...AND), And more efficient

Usually , In preservation IPv4 Address time , One IPv4 Minimum need 7 Characters , The biggest need 15 Characters , therefore , Use VARCHAR(15) that will do .MySQL When saving variable length strings , An additional byte is required to hold the length of this string . If you use unsigned integers to store , It only needs 4 Only bytes .

You can also use it 4 The fields are stored separately IPv4 The parts of , But generally, both storage space and query efficiency should not be very high ( There may be some scenarios that are suitable for storing ).

Use strings and unsigned integers to store IP Specific performance analysis and benchmark, You can read this article .

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

There are also disadvantages to using unsigned integers to store :

  • Not easy to read

  • You need to manually switch

For conversion ,MySQL The corresponding functions are provided to convert the string format IP Convert to integer INET_ATON, And the integer format IP Converted to a string INET_NTOA. As shown below :

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)

about IPv6 Come on , Use VARBINARY The same benefits can be obtained , meanwhile MySQL The corresponding conversion function is also provided , namely INET6_ATON and INET6_NTOA. In the past :300 I'm going to have an interview

For conversion strings IPv4 And numerical types , It can be placed on the application layer , Here's how to use java Code to convert the two :

package com.mikan;  
   
/**  
 * @author Mikan  
 */  
public class IpLongUtils {  
    /**  
     *  Put the string IP convert to long  
     *  
     * @param ipStr  character string IP  
     * @return IP Corresponding long value   
     */  
    public static long ip2Long(String ipStr) {  
        String[] ip = ipStr.split("\\.");  
        return (Long.valueOf(ip[0]) << 24) + (Long.valueOf(ip[1]) << 16)  
                + (Long.valueOf(ip[2]) << 8) + Long.valueOf(ip[3]);  
    }  
   
    /**  
     *  hold IP Of long Value to string   
     *  
     * @param ipLong IP Of long value   
     * @return long The string corresponding to the value   
     */  
    public static String long2Ip(long ipLong) {  
        StringBuilder ip = new StringBuilder();  
        ip.append(ipLong >>> 24).append(".");  
        ip.append((ipLong >>> 16) & 0xFF).append(".");  
        ip.append((ipLong >>> 8) & 0xFF).append(".");  
        ip.append(ipLong & 0xFF);  
        return ip.toString();  
    }  
   
    public static void main(String[] args) {  
        System.out.println(ip2Long("192.168.0.1"));  
        System.out.println(long2Ip(3232235521L));  
        System.out.println(ip2Long("10.0.0.1"));  
    }  
      
}

The output is :

3232235521  
192.168.0.1  
167772161

source :blog.csdn.net/mhmyqn/article/details/48653157

recommend :

The most comprehensive java Interview question bank

3340af85c62098801645b76e1c226977.png

PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !

原网站

版权声明
本文为[Java notes shrimp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110718146615.html