利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方
网络编程 2021-07-05 15:27www.168986.cn编程入门
当前很多应用都适用字符串char(15)来存储IP地址(占用16个字节),利用i_aton()和i_ntoa()函数,来存储IP地址效率很高,适用unsigned int 就可以满足需求,不需要使用bigint,只需要4个字节,节省存储空间,效率也高很多
mysql> create table jackbillow (ip int unsigned, name char(1));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into jackbillow values(i_aton('192.168.1.200'), 'A'), (i_aton('200.100.30.241'), 'B');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into jackbillow values(i_aton('24.89.35.27'), 'C'), (i_aton('100.200.30.22'), 'D');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select from jackbillow;
+------------+------+
| ip | name |
+------------+------+
| 3232235976 | A |
| 3362004721 | B |
| 408494875 | C |
| 1690836502 | D |
+------------+------+
4 rows in set (0.00 sec)
mysql> select from jackbillow where ip = i_aton('192.168.1.200');
+------------+------+
| ip | name |
+------------+------+
| 3232235976 | A |
+------------+------+
1 row in set (0.00 sec)
mysql> select i_ntoa(ip) from jackbillow;
+----------------+
| i_ntoa(ip) |
+----------------+
| 192.168.1.200 |
| 200.100.30.241 |
| 24.89.35.27 |
| 100.200.30.22 |
+----------------+
4 rows in set (0.00 sec)
当前很多应用都适用字符串char(15)来存储IP地址(占用16个字节),利用i_aton()和i_ntoa()函数,来存储IP地址效率很高,适用unsigned int 就可以满足需求,不需要使用bigint,只需要4个字节,节省存储空间,效率也高很多。
如果IP列有索引,可以使用狼蚁网站SEO优化方式查询
mysql> select i_aton('100.200.30.22');
+----------------------------+
| i_aton('100.200.30.22') |
+----------------------------+
| 1690836502 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select from jackbillow where ip=1690836502;
+------------+------+
| ip | name |
+------------+------+
| 1690836502 | D |
+------------+------+
1 row in set (0.00 sec)
mysql> select i_ntoa(ip),name from jackbillow where ip=1690836502;
+---------------+------+
| i_ntoa(ip) | name |
+---------------+------+
| 100.200.30.22 | D |
+---------------+------+
1 row in set (0.00 sec)
对于LIKE操作,可以使用狼蚁网站SEO优化方式
mysql> select i_ntoa(ip) from jackbillow;
+----------------+
| i_ntoa(ip) |
+----------------+
| 192.168.1.200 |
| 200.100.30.241 |
| 24.89.35.27 |
| 100.200.30.22 |
| 192.168.1.100 |
| 192.168.1.20 |
| 192.168.2.20 |
+----------------+
7 rows in set (0.00 sec)
mysql> select i_aton('192.168.1.0');
+--------------------------+
| i_aton('192.168.1.0') |
+--------------------------+
| 3232235776 |
+--------------------------+
1 row in set (0.00 sec)
mysql> select i_aton('192.168.1.255');
+----------------------------+
| i_aton('192.168.1.255') |
+----------------------------+
| 3232236031 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select i_ntoa(ip) from jackbillow where ip between 3232235776 and 3232236031;
+---------------+
| i_ntoa(ip) |
+---------------+
| 192.168.1.200 |
| 192.168.1.100 |
| 192.168.1.20 |
+---------------+
3 rows in set (0.00 sec)
mysql> select i_ntoa(ip) from jackbillow where ip between i_aton('192.168.1.0') and i_aton('192.168.1.255');
+---------------+
| i_ntoa(ip) |
+---------------+
| 192.168.1.200 |
| 192.168.1.100 |
| 192.168.1.20 |
+---------------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
mysql> insert into jackbillow values(i_aton('192.168.1.200'), 'A'), (i_aton('200.100.30.241'), 'B');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into jackbillow values(i_aton('24.89.35.27'), 'C'), (i_aton('100.200.30.22'), 'D');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select from jackbillow;
+------------+------+
| ip | name |
+------------+------+
| 3232235976 | A |
| 3362004721 | B |
| 408494875 | C |
| 1690836502 | D |
+------------+------+
4 rows in set (0.00 sec)
mysql> select from jackbillow where ip = i_aton('192.168.1.200');
+------------+------+
| ip | name |
+------------+------+
| 3232235976 | A |
+------------+------+
1 row in set (0.00 sec)
mysql> select i_ntoa(ip) from jackbillow;
+----------------+
| i_ntoa(ip) |
+----------------+
| 192.168.1.200 |
| 200.100.30.241 |
| 24.89.35.27 |
| 100.200.30.22 |
+----------------+
4 rows in set (0.00 sec)
当前很多应用都适用字符串char(15)来存储IP地址(占用16个字节),利用i_aton()和i_ntoa()函数,来存储IP地址效率很高,适用unsigned int 就可以满足需求,不需要使用bigint,只需要4个字节,节省存储空间,效率也高很多。
如果IP列有索引,可以使用狼蚁网站SEO优化方式查询
mysql> select i_aton('100.200.30.22');
+----------------------------+
| i_aton('100.200.30.22') |
+----------------------------+
| 1690836502 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select from jackbillow where ip=1690836502;
+------------+------+
| ip | name |
+------------+------+
| 1690836502 | D |
+------------+------+
1 row in set (0.00 sec)
mysql> select i_ntoa(ip),name from jackbillow where ip=1690836502;
+---------------+------+
| i_ntoa(ip) | name |
+---------------+------+
| 100.200.30.22 | D |
+---------------+------+
1 row in set (0.00 sec)
对于LIKE操作,可以使用狼蚁网站SEO优化方式
mysql> select i_ntoa(ip) from jackbillow;
+----------------+
| i_ntoa(ip) |
+----------------+
| 192.168.1.200 |
| 200.100.30.241 |
| 24.89.35.27 |
| 100.200.30.22 |
| 192.168.1.100 |
| 192.168.1.20 |
| 192.168.2.20 |
+----------------+
7 rows in set (0.00 sec)
mysql> select i_aton('192.168.1.0');
+--------------------------+
| i_aton('192.168.1.0') |
+--------------------------+
| 3232235776 |
+--------------------------+
1 row in set (0.00 sec)
mysql> select i_aton('192.168.1.255');
+----------------------------+
| i_aton('192.168.1.255') |
+----------------------------+
| 3232236031 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select i_ntoa(ip) from jackbillow where ip between 3232235776 and 3232236031;
+---------------+
| i_ntoa(ip) |
+---------------+
| 192.168.1.200 |
| 192.168.1.100 |
| 192.168.1.20 |
+---------------+
3 rows in set (0.00 sec)
mysql> select i_ntoa(ip) from jackbillow where ip between i_aton('192.168.1.0') and i_aton('192.168.1.255');
+---------------+
| i_ntoa(ip) |
+---------------+
| 192.168.1.200 |
| 192.168.1.100 |
| 192.168.1.20 |
+---------------+
3 rows in set (0.00 sec)
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程