Method for storing IP addresses in MySQL _ MySQL

Source: Internet
Author: User
Tags ip number
This article describes how to store IP addresses in MySQL to maximize performance. For more information, see Why is IP storage required?

First, let's clarify some people's questions: why do I have to know how to store IP addresses? isn't the direct varchar type?

In fact, any program design should optimize performance to the maximum extent on the basis of function implementation. Database Design is an important part of programming, so the storage IP address can be greatly improved to a certain extent.

Processing Using function algorithms

The IP type field is not directly provided in MySQL. However, if there are two functions that can convert the IP address and the maximum length to 10 digits, therefore, the performance of int-type storage IP addresses is much higher than that of varchar storage IP addresses, reducing the storage space. Because varchar is a variable length, an extra byte storage length is required. In addition, int type is faster than varchar in logical operation.


IP to Digital function inet_aton ()

We can convert several common IP addresses.

 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)

Therefore, you can set the IP table field to INT (10). If the IP address cannot be obtained, you can directly store 0 to indicate that the IP address cannot be obtained.

Number to IP function 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)

Note: 0 is converted to 0.0.0.0

Integer fields are much more efficient than strings, which also conforms to an optimization principle: the most appropriate (minimum) and simplest data type for field type definition.
The inet_aton () algorithm actually uses the ip number used in international ip address differentiation.
The ip number of a. B. c. d is:
A * 256 power 3 + B * 256 power 2 + c * 256 power 1 + d * 256 power 0.

The above explains how to store IP addresses in MySQL, hoping to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.