ip地址使用int类型存储,用INET_NTOA()和INET_ATON()转换

mysql> select inet_ntoa(''),inet_aton('127.0.0.1');
+-------------------------+------------------------+
| inet_ntoa('') | inet_aton('127.0.0.1') |
+-------------------------+------------------------+
| 127.0.0.1 | 2130706433 |
+-------------------------+------------------------+
1 row in set (0.00 sec)

1.环境

mysql ----5.6.13

mysql> show create table test \G;
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`ip_from` int(10) unsigned DEFAULT NULL,
`ip_to` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_ip` (`ip_from`,`ip_to`),
KEY `idx_ip_from` (`ip_from`)
) ENGINE=InnoDB AUTO_INCREMENT=9568111 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.01 sec)
ERROR:
No query specified ------------------------------------------------------
mysql> show index from test;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test | 0 | PRIMARY | 1 | id | A | 9289578 | NULL | NULL | | BTREE | | |
| test | 1 | idx_ip | 1 | ip_from | A | 9289578 | NULL | NULL | YES | BTREE | | |
| test | 1 | idx_ip | 2 | ip_to | A | 9289578 | NULL | NULL | YES | BTREE | | |
| test | 1 | idx_ip_from | 1 | ip_from | A | 9289578 | NULL | NULL | YES | BTREE | | |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec) mysql> select count(*) from test;
+----------+
| count(*) |
+----------+
| 9541210 |
+----------+
1 row in set (2.84 sec)

2.使用

查询某个值属于哪个ip段。

  • SELECT * FROM test WHERE ip_from<=2352356 AND ip_to>=2352356;
mysql> explain SELECT * FROM test WHERE ip_from<=2352356 AND ip_to>=2352356;
+----+-------------+-------+-------+--------------------+--------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+--------------------+--------+---------+------+------+-----------------------+
| 1 | SIMPLE | test | range | idx_ip,idx_ip_from | idx_ip | 5 | NULL | 1 | Using index condition |
+----+-------------+-------+-------+--------------------+--------+---------+------+------+-----------------------+
1 row in set (0.08 sec)
  • 这个方式对索引进行了范围全扫描,耗时较长。
  • SELECT * FROM test WHERE id IN ( SELECT id FROM test WHERE ip_from<=2352356 AND ip_to>=2352356 );
mysql> EXPLAIN SELECT * FROM test WHERE id IN (
-> SELECT id FROM test WHERE ip_from<=2352356 AND ip_to>=2352356 );
+----+-------------+-------+--------+----------------------------+---------+---------+---------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+----------------------------+---------+---------+---------------------+------+--------------------------+
| 1 | SIMPLE | test | range | PRIMARY,idx_ip,idx_ip_from | idx_ip | 5 | NULL | 1 | Using where; Using index |
| 1 | SIMPLE | test | eq_ref | PRIMARY | PRIMARY | 8 | ip2location.test.id | 1 | NULL |
+----+-------------+-------+--------+----------------------------+---------+---------+---------------------+------+--------------------------+
2 rows in set (0.01 sec)
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.2
Connection id: 4305567
Current database: ip2location
Current user: ip2location@10.1.255.10
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.13-log MySQL Community Server (GPL)
Protocol version: 10
Connection: ip2location.cgs2bjzqxcxl.us-east-1.rds.amazonaws.com via TCP/IP
Insert id: 1
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 30 days 18 hours 51 min 44 sec
Threads: 4 Questions: 21017670 Slow queries: 4 Opens: 188007 Flush tables: 1 Open tables: 147 Queries per second avg: 7.901 --------------------------------------------- mysql> EXPLAIN SELECT * FROM test WHERE id IN (
-> SELECT id FROM test WHERE ip_from<=2352356 AND ip_to>=2352356 );
+----+--------------------+-------+-----------------+----------------------------+---------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-----------------+----------------------------+---------+---------+------+--------+-------------+
| 1 | PRIMARY | test | ALL | NULL | NULL | NULL | NULL | 206509 | Using where |
| 2 | DEPENDENT SUBQUERY | test | unique_subquery | PRIMARY,idx_ip,idx_ip_from | PRIMARY | 8 | func | 1 | Using where |
+----+--------------------+-------+-----------------+----------------------------+---------+---------+------+--------+-------------+
2 rows in set (0.00 sec)
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.37, for Linux (x86_64) using EditLine wrapper
Connection id: 5
Current database: howe
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.37-log Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /home/mysql/mysql5/tmp/mysql.sock
Uptime: 6 min 52 sec
Threads: 3 Questions: 208 Slow queries: 0 Opens: 112 Flush tables: 1 Open tables: 105 Queries per second avg: 0.504
--------------

不同版本对IN的处理方式不同,5.6优于以前的版本

  • SELECT * FROM test WHERE ip_from<=2352356 ORDER BY ip_from DESC LIMIT 1;
mysql> explain SELECT * FROM test WHERE ip_from<=2352356 ORDER BY ip_from DESC LIMIT 1;
+----+-------------+-------+-------+--------------------+--------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+--------------------+--------+---------+------+------+-----------------------+
| 1 | SIMPLE | test | range | idx_ip,idx_ip_from | idx_ip | 5 | NULL | 1 | Using index condition |
+----+-------------+-------+-------+--------------------+--------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

删除idx_ip索引。

mysql> explain SELECT * FROM test WHERE ip_from<=2352356 ORDER BY ip_from DESC LIMIT 1;
+----+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------+
| 1 | SIMPLE | test | range | idx_ip_from | idx_ip_from | 5 | NULL | 1 | Using index condition |
+----+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

这个方式是最优。利用了ip段的特性、order by、limit。

IP地址的存储和使用的更多相关文章

  1. IP地址转换成Long型数字的算法

    在应用程序开发中,涉及到IP地址的存储,大部分开发人员都将其存为String(或文本类型).能否将固定格式为m.n.x.y的IP地址转换成 Long型的数字呢?答案是肯定的.在数据库层面,可以直接将结 ...

  2. Linux下IP SAN共享存储操作记录

    一.简单介绍SAN,即存储区域网络(storage area network and SAN protocols),它是一种高速网络实现计算机与存储系统之间的数据传输.常见的分类是FC-SAN和IP- ...

  3. 【mysql】IP地址整数int和varchar的转换

    mysql中IP地址的存储 IP:如192.168.12.145,在存储时,若是采用varchar进行存储,存在两个主要缺点: 存储空间占用较大: 查询检索较慢: 解决方式: 存储时:将字符串类型的I ...

  4. IP地址在数据库里面的存储方式

    大多数公司的表结构都需要经过DBA进行审核,有时候你会看到存储IP地址采用varchar(15),这种方式都是传统的做法,这种方法需要占用15个字节,那么有更省空间的做法么?肯定是有的,那就是用int ...

  5. 【mysql】MySQL存储IP地址

    为什么要问如何存储IP 首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗? 其实做任何程序设计都要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不 ...

  6. 如何在数据库中存储IP地址

    最近改一个比较老的web系统,该系统是通过账号或者ip地址(白名单)验证限制访问权限的. 由于运营的时间比较长了,发现进入网站巨卡... 原因就是:之前的数据库(sqlserver)存储ip地址是用的 ...

  7. mysql 存储ip地址

    mysql提供了两个方法来处理ip地址: inet_aton 把ip转为无符号整型(4-8位) inet_ntoa 把整型的ip转为电地址 插入数据前,先用inet_aton把ip地址转为整型,可以节 ...

  8. IP地址在mysql的存储(IP地址和int的转换)

    PHP echo ip2long('192.168.1.38'); 输出:3232235814 MYSQL SELECT INET_ATON('192.168.1.38'); 输出:323223581 ...

  9. MySQL怎样存储IP地址 IP转数字 互转

    MySQL怎样存储IP地址 - cn三少 - 博客园 https://www.cnblogs.com/cnsanshao/p/3326648.html

随机推荐

  1. lnmp下安装ffmpeg和ffmpeg-php教程

    现在我将我的过程方法发布出来. 以下都是用SSH命令 一.安装ffmpeg 操作系统:centos6 安装ffmpeg有两种方式:①.用源码包安装,这个不知道怎么回事老是报错②用yum命令安装,cen ...

  2. gcc使用笔记

    1.如何在gcc中传输宏定义? 参考如下红色部分,可以传入宏定义 gcc [-c|-S|-E] [-std=standard] [-g] [-pg] [-Olevel] [-Wwarn...] [-p ...

  3. DOS头 IMAGE_DOS_HEADER

    IMAGE_DOS_HEADER STRUCT { +0h WORD e_magic // Magic DOS signature MZ(4Dh 5Ah) DOS可执行文件标记 +2h WORD e_ ...

  4. 高性能Socket组件和RPC,让你像写本地代码一样开发网络应用和分布式程序

    最近想试试C#能不能写出高性能的分布式组件,于是写了一个双工RPC,也当练手,下面是单连接的测试结果,非常给力.机器配置:U:E1230-v2,内存:ddr3-8G 1.递归调用    过程:Clie ...

  5. 06 - 从Algorithm 算法派生类中删除ExecuteInformation() 和ExecuteData() VTK 6.0 迁移

    在先前的vtk中,如vtkPointSetAlgorithm 等算法派生类中定义了虚方法:ExecuteInformation() 和 ExecuteData().这些方法的定义是为了平稳的从VTK4 ...

  6. RAILS局部视图操作样例

    按如下书操作的,讲得很易懂.. <html> <head> <title>SampleApp</title> <%= stylesheet_lin ...

  7. B450配置

    电脑型号 联想 B450 笔记本电脑  (扫描时间:2015?5?7?操作系统 Windows 7 旗舰版 32位 SP1 ( DirectX 11 ) 处理器 英特尔 酷睿2 双核 T9800 @ ...

  8. 【转】随身HiFi 安卓OTG功能在音频上的妙用

    原文网址:http://article.pchome.net/content-1745467.html 随身HiFi 安卓OTG功能在音频上的妙用 [PChome电脑之家音频频道原创]说起Androi ...

  9. HDU_2047——EOF字符串排序排列问题,递推

    Problem Description 今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级的COY组成.在共同的集训生活中,大家建立了深厚的友 ...

  10. POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12276   Accepted: 3886 Description ...