I am confused. If I want to store IP addresses, what data type is better?

I am confused. If I want to store IP addresses, what data type is better?

When it comes to IP addresses (IPv4), common IP addresses such as 192.168.0.1 and 127.0.0.1 will immediately come to mind. Then, considering the question "What data type is used to store IP addresses in MySQL?", the char string type will be used.

The interviewer looked indifferent, and you immediately realized that something was wrong and thought about it carefully.

Then I found that the length of this IP address varies. The shortest can be 0.0.0.0, which only requires 7 bits, and the longest can be 255.255.255.255, which requires 15 bits. So I confidently answered to use varchar(15) to store the IP address, and secretly delighted myself that I could think of this.

Unexpectedly, the interviewer smiled contemptuously and asked you "Are you sure?" You thought that the interviewer was testing you, so you answered firmly "Yes".

Then we started the next question

......

People often use varchar(15) columns to store IP addresses, but this is not the optimal solution.

The essence of an IP address is a 32-bit unsigned integer. The dotted decimal string format like 192.168.0.1 is just to help people understand and remember. The decimal representation of 192.168.0.1 is the unsigned integer 3232235521.

Therefore, when people say that IP addresses are stored in string type, they actually subconsciously think that IP addresses are strings and store dotted decimal strings, but the correct method is to store 32-bit unsigned integers.

The so-called signed number actually uses the highest bit as the sign bit. For example, for a 32-bit signed INT, the highest bit is the sign bit, and the remaining 31 bits are the actual value. Therefore, the value range of a signed INT is:

The value range of unsigned INT is:

The following table lists the signed and unsigned value ranges of various integer types in MySQL. When defining a table, you can add the keyword UNSIGNED after the data type to define an unsigned integer, otherwise the default is a signed integer:

type

Signed number value range

Unsigned value range

TINYINT (1 byte, 8 bits)

-128 ~ 127

0 ~ 255

SMALLINT (2 bytes, 16 bits)

-32768 ~ 32767

0 ~ 65535

MEDIUMINT (3 bytes, 24 bits)

-8388608 ~ 8388607

0 ~ 16777215

INT (4 bytes, 32 bits)

-2147483648 ~ 2147483647

0 ~ 4294967295

BIGINT (8 bytes, 64 bits)

-9223372036854775808 ~ 9223372036854775807

0 ~ 18446744073709551615

Combined with the above table, we can see that a 32-bit unsigned INT can just hold an IPv4 address. The following is a comparison between the two data types: INT UNSIGNED and VARCHAR(15):

  1. Storage space: 4-byte INT type and 15-byte VARCHAR(15) save more storage space. In addition, VARCHAR not only saves the required number of characters, but also adds one byte to record the length (if the length declared for the column exceeds 255, two bytes are used to record the length), so VARCHAR(15) actually takes up 16 bytes.
  2. Retrieval speed: If we want to create an index on the IP address, then for the string index, the retrieval speed of the integer index is simply a downgrade, because the comparison of the string type needs to start from the first character and traverse in sequence, which is slower.

MySQL very considerately provides the conversion functions between IPv4 address dotted decimal and unsigned integer, inet_aton and inet_ntoa (the underlying operation is binary shift, which is very fast):

Of course, you should perform these conversions in your business to reduce the pressure on MySQL.

<<:  A magical little tool that turns URL addresses into "ooooooooo"

>>:  Application of 5G in the Public Sector of Future Smart Cities

Recommend

Build a distributed IM (Instant Messaging) system yourself

I have shared an article "Designing a ***...

The "Six Mountains" that Block Operator Innovation

The recent discussion about the advanced construc...

Five best practices for improving network uptime

In the near future, no one should have to wait fo...

Canceling traffic "roaming": Will there be 93 operators in China?

At the first session of the 13th National People&...

Don't understand the network I/O model? How to get started with Netty

Netty is a network application framework, so from...

Network Access Control-Network Address Translation

With the development of the Internet and the incr...

RackNerd: San Jose data center VPS promotion starting at $11.88/year

A week ago, we shared RackNerd's promotional ...

New breakthrough! WiFi 7 is coming soon…

On February 15 , Qualcomm's official website ...

Do you know which city has the fastest Wi-Fi speed in the world?

Since the coronavirus crisis, fast internet has b...

Woman connected to WiFi and received a huge bill: Some WiFi is actually charged

Nowadays, surfing the Internet with mobile termin...

Let’s talk about how IP addresses are allocated?

In the IPV4 era, IP addresses are a scarce resour...