当前位置:网站首页>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
167772161source :blog.csdn.net/mhmyqn/article/details/48653157
recommend :
The most comprehensive java Interview question bank

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 !
边栏推荐
- Niuke wrong question 3.1
- Oracle pl/sql these query results cannot be updated. Please include ROWID or use Select For update
- WPF data binding (IV)
- Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
- 农房一体脚本的心得记录
- Concurrent tool class
- Saltstack deployment ZABBIX state file writing
- R语言并行计算实战教程
- [STL source code analysis] summary notes (5): a good helper for understanding iterators --list
- Esp32 learning notes (49) - esp-wifi-mesh interface use
猜你喜欢

Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)

【LeetCode】-- 17. Letter combination of telephone number

生物序列智能分析平台blog(1)

一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试

Listen to the left width of the browser to calculate the distance

一、SQLServer2008安裝(帶密碼)、創建數據庫、C#窗體項目測試

2022低压电工考题及在线模拟考试

QT interface nested movement based on qscrollarea

【LeetCode】-- 17.电话号码的字母组合

Nodejs database (Part 2)
随机推荐
Oracle pl/sql these query results cannot be updated. Please include ROWID or use Select For update
服务器调参实录
WPF data binding (IV)
421. maximum XOR value of two numbers in the array
【LeetCode】-- 17.电话号码的字母组合
554. brick wall
Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
big. Js-- use / instance
Gobang interface of mobile console (C language)
[advanced concurrency] - thread pool summary
Concurrent tool class
教育专家王中泽老师:家庭教育重在自己成长
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
P3172 [cqoi2015] data selection (Mobius inversion + Du Jiao sieve)
2、 User login and registration
多线程复习总结之解析Volatile关键字
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
MS office level II wrong question record [10]
資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員