What type should I store IP addresses for MySQL?

前端 未结 3 901
遇见更好的自我
遇见更好的自我 2020-12-03 15:34

I was going to use varchar(20), but I was wondering what should if I should do INT and strip off the periods instead. What would be better and why?

3条回答
  •  既然无缘
    2020-12-03 15:54

    If you're really concerned about saving space, you can pack it into a 4-byte int. Each block of an IPv4 IP address can have 256 possible values, which just happens to be the range of a single byte.

    -- Edit --> What I just described can be done with the mysql functions f00 linked in his answer

    However, storing it as a string will save a bit of coding time. Honestly, unless you're doing this on a very large scale, the optimization from a denser storage format won't matter.

提交回复
热议问题