What type of data to store ip addresses of users in SQL MySQL?
- oneIt depends on what you are going to do with it and how many of these records, if you just look at it, then it is possible to have a varchar, if you filter on it, it is better to use an integer. If there are a lot of records, then the number will also take up less space - cache
- @cache will be sampled by IP. We will find one IP. Column where will be recorded IP, with an index of unique values. - gilo1212
- @ gilo1212, if so then look at the supplement in my answer, these functions convert the aypishnik to a number - it is more convenient to operate on more details here expange.ru/e/… - Let 's run Boris
- one@ Boris may run better this way - BINARY (16) INET6_ATON and INET6_NTOA and respectively - INET_NTOA () and INET6_ATON? - gilo1212 2:58 pm
- 2@ gilo1212 then the format should be uniquely numeric, not varchar. Here the author of the answer advises using VARBINARY (16) instead of BINARY (16) on stackoverflow.com/questions/6964143/… to support IPV4 + IPV6 - cache
3 answers
IPv4 addresses are stored in the int unsigned int field as a number. Unsigned it is necessary that one sign more intermeddle.
On the hard disk takes 4 bytes. Which is less than char / varchar.
To convert from a string IP address to a number, there is the mysql function INET_ATON :
mysql> SELECT INET_ATON('193.125.99.10'); +----------------------------+ | INET_ATON('193.125.99.10') | +----------------------------+ | 3246220042 | +----------------------------+ 1 row in set (0.00 sec) To convert from number to address - INET_NTOA .
- Int holds up to 2147483647
- Int unsigned - up to 4294967295
Sampling by ranges is convenient. My source :)
Store ip data in varchar - moveton, suitable only for small projects. If you have a million visitors a day, you need to squeeze out maximum performance from the database, and use as little memory as possible. Since memory is money, processor cycles are money.
A million visitors per day may well be if you do not shop, but, say, an advertising grid, dsp, banner twisting, payment system (your own or for the bank), etc.
Millions of visitors per day - for example, your start-up starcher who has been deployed on AWS, an extra hundred (one thousand?) Bucks a month that such optimization can never save you.
Using INET_NTOA / INET_ATON is very easy, but important optimization.
When you need to refer to the database for an IP address, it is better to do inet_ntoa / inet_aton transformations before the request, in php / python / on_ what_you_tam_write to use database indices.
PS about IPV6 - https://dba.stackexchange.com/a/81402 in version 5.6 MySQL added functions to work with it
- fourAnd if someone wants to log in with IPv6? - gilo1212
- @ gilo1212 Then do not use unsigned int. IpV6 will be more likely if only in local networks, and behind ip there will be a regular v4 ip. In varchar (15), it still does not fit, it is necessary varchar (39) at least. - jekaby
- And what are the cons, what is the mistake? - jekaby
- The answer, though supplemented, does not say how to store both IPv4 and IPv6 in one field, and what type this field should be. - nzeemin
- Most likely - stackoverflow.com/a/6965026/5006740 , but I would povenchmarkil for myself. preferably on a real load. - strangeqargo
we store in varchar, in principle, why invent something to store an IP address?
You can of course be stored as integers and use the functions mysql INET_ATON () and INET_NTOA ().
- 3For some reason it seems to me that this is not entirely correct - gilo1212
- we also store in varchar - ikerya
- why? This is the standard version ... - Boris Runs
- So the choice will take a long time, compared with BINARY (16) - gilo1212
- oneAnd how to store ipv6? - gilo1212
4 byte fields for IPv4
And it takes less space and it is easier to operate
- Is it really such a bad option? so many cons ... - 4per
- @ 3per himself in shock. Well, at least commented on what exactly is not like. - rjhdby