当前位置:网站首页>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 !
边栏推荐
- matplotlib的cmap
- 資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
- 12. integer to Roman numeral
- [STL source code analysis] summary notes (5): a good helper for understanding iterators --list
- MS office level II wrong question record [9]
- big. Js-- use / instance
- QT interface nested movement based on qscrollarea
- P1390 sum of common divisors (Mobius inversion)
- Decimal to binary
- Directrix of ellipse
猜你喜欢

Niuke wrong question 3.1
![[并发进阶]——线程池总结](/img/69/dc8146dafc30f8a8efa012b67aa05c.png)
[并发进阶]——线程池总结

教育专家王中泽老师:家庭教育重在自己成长

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

二、用户登录和注册

Leetcode-104. Maximum Depth of Binary Tree
![[STL source code analysis] summary note (2): overview of containers](/img/66/60fba564ae6020dfb503c7fdf78529.jpg)
[STL source code analysis] summary note (2): overview of containers

2、 User login and registration

Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
随机推荐
Library management system 1- project approval
big.js--使用/实例
Detailed explanation of mutationobserver
【CF#388 (Div. 2)】A. Bachgold Problem
【CF】 A. New Year Candles
Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration
資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
Method to determine whether it is an array
Regular Expression Matching
Biological sequence intelligent analysis platform blog (1)
Analysis of key points and difficulties of ES6 promise source code
1190. invert the substring between each pair of parentheses
Common modules of saltstack
Decimal to binary
Leetcode-104. Maximum Depth of Binary Tree
Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
AtomicInteger原子操作类
Promise details
1442. number of triples forming two exclusive or equal arrays
Promises/a+ standard Chinese Translation