小编RMa*_*his的帖子

确定 IP 是否在 IPv4 CIDR 块内

确定 IP 是否包含在 CIDR 块中的最快方法是什么?

目前,每当我存储 CIDR 地址时,我还会为起始和结束 IP 地址创建两列。开始和结束 ip 地址已编入索引。如果我想查看哪个网络包含地址,那么我看起来where ip between start_ip and end_ip似乎不太理想。

在我看来,我可以存储正确移位的数字,并且可以匹配类似移位的 IP 地址(@cidr 的情况下为 660510)...

select @cidr, inet_aton(substring_index(@cidr,'/',1))>>(32-substring_index(@cidr,'/',-1));
+---------------+-----------------------------------------------------------------------------+
| @cidr         | inet_aton(substring_index(@cidr,'/',1))>>(32-substring_index(@cidr,'/',-1)) |
+---------------+-----------------------------------------------------------------------------+
| 10.20.30.0/24 |                                                                      660510 |
+---------------+-----------------------------------------------------------------------------+
1 row in set (0.00 sec)

set @ip:='10.20.30.40';
Query OK, 0 rows affected (0.00 sec)

select @ip, inet_aton(@ip)>>(32-substring_index(@cidr,'/',-1));
+-------------+----------------------------------------------------+
| @ip         | inet_aton(@ip)>>(32-substring_index(@cidr,'/',-1)) |
+-------------+----------------------------------------------------+
| 10.20.30.40 |                                             660510 |
+-------------+----------------------------------------------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

为了以索引方式从中受益,我需要知道子网掩码(要移动的位数)。否则,我要么系统地比较位移位(即,盲目位移每个可能的网络掩码(从 0 位到 …

mysql index datatypes range-types

6
推荐指数
2
解决办法
4921
查看次数

标签 统计

datatypes ×1

index ×1

mysql ×1

range-types ×1