Int or varchar (15) is recommended for IP address storage. Is the efficiency different?

Source: Internet
Author: User
Some database structure ip addresses are stored in int type and converted using ip2long. However, this function seems to have a bug. What do you think? Some database structure ip addresses are stored in int type and converted using ip2long. However, this function seems to have a bug. What do you think?

Reply content:

Some database structure ip addresses are stored in int type and converted using ip2long. However, this function seems to have a bug. What do you think?

Mysql can refer to this answer.

Postgres directly belongs to the network address type. For more information, see here.

Use varchar or char
Reason:
1. Saving the conversion process
2. Both ipv4 and ipv6 can be stored.

It is not good to store both types.

Generally, a website simply uses varchar (15) to record user information.
Int is generally used for ip database, and it is easy to query the province and city of an ip address by segment record.

Use int to save space. Only four bytes are used. Use 7 to char ~ 15 bytes.
The most important thing is convenient query and comparison (numerical comparison), and setting an integer as an index is also more efficient than char.

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.