storing/retrieving IPv4 & IPv6 addresses in MySQL

末鹿安然 提交于 2019-12-06 16:24:41
Xavjer

I would go with this: citat from there : How to store IPv6-compatible address in a relational database "Final decision taken: 2xBIGINT if the second bigint is NULL, then it means IPv4"

It sounds like your main concern is about space. If that's the case, then you could use the fact that IPv4 addresses are (essentially) 32-bit numbers and IPv6 are 128. An IPv4 address can be stored in an INT column, but IPv6 would require two BIGINT columns in MySQL. This is likely to be much more space-efficient than storing strings.

The cost of doing this is that you need to do the conversion from address -> number before inserting the value into the database. That will (slightly) increase CPU load on your web server, so you need to figure out where your bottleneck is going to be and optimise for that.

An added benefit of storing the addresses as numbers is that you can have a very efficient index on the column(s) so looking up an address will be lightning fast. Indexing varchar columns is very expensive.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!