IP地址的存储和使用
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地址的存储和使用的更多相关文章
- IP地址转换成Long型数字的算法
在应用程序开发中,涉及到IP地址的存储,大部分开发人员都将其存为String(或文本类型).能否将固定格式为m.n.x.y的IP地址转换成 Long型的数字呢?答案是肯定的.在数据库层面,可以直接将结 ...
- Linux下IP SAN共享存储操作记录
一.简单介绍SAN,即存储区域网络(storage area network and SAN protocols),它是一种高速网络实现计算机与存储系统之间的数据传输.常见的分类是FC-SAN和IP- ...
- 【mysql】IP地址整数int和varchar的转换
mysql中IP地址的存储 IP:如192.168.12.145,在存储时,若是采用varchar进行存储,存在两个主要缺点: 存储空间占用较大: 查询检索较慢: 解决方式: 存储时:将字符串类型的I ...
- IP地址在数据库里面的存储方式
大多数公司的表结构都需要经过DBA进行审核,有时候你会看到存储IP地址采用varchar(15),这种方式都是传统的做法,这种方法需要占用15个字节,那么有更省空间的做法么?肯定是有的,那就是用int ...
- 【mysql】MySQL存储IP地址
为什么要问如何存储IP 首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗? 其实做任何程序设计都要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不 ...
- 如何在数据库中存储IP地址
最近改一个比较老的web系统,该系统是通过账号或者ip地址(白名单)验证限制访问权限的. 由于运营的时间比较长了,发现进入网站巨卡... 原因就是:之前的数据库(sqlserver)存储ip地址是用的 ...
- mysql 存储ip地址
mysql提供了两个方法来处理ip地址: inet_aton 把ip转为无符号整型(4-8位) inet_ntoa 把整型的ip转为电地址 插入数据前,先用inet_aton把ip地址转为整型,可以节 ...
- IP地址在mysql的存储(IP地址和int的转换)
PHP echo ip2long('192.168.1.38'); 输出:3232235814 MYSQL SELECT INET_ATON('192.168.1.38'); 输出:323223581 ...
- MySQL怎样存储IP地址 IP转数字 互转
MySQL怎样存储IP地址 - cn三少 - 博客园 https://www.cnblogs.com/cnsanshao/p/3326648.html
随机推荐
- DbContext的命名空间
未能找到类型或命名空间名称“DbContext” 解决办法:需要引用命名空间,using System.Data.Entity; 如果还报错的话,则项目需要引用程序集EntityFramework.d ...
- git “bad index file sha1 signature fatal: index file corrupt”错误
在执行commit或revert等操作时,提示“bad index file sha1 signature fatal: index file corrupt”错误,导致操作失败.这是由于git的in ...
- Linux sar使用
[root@ywcrmdb ~]# sar -d 1 10 Linux 2.6.32-220.el6.x86_64 (ywcrmdb) 2014年04月19日 _x86_64_ (4 CPU) 1 ...
- Qt入门(1)——初识Qt
Qt是一个跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊的代码生成扩展(称为元对象编译器(Meta ...
- linux下编译eXosip、osip,以及UAC和UAS的例子
从网站上看到了这样的一篇博文 :Windows下编译eXosip.osip,以及UAC和UAS的例子 (链接:http://www.cnblogs.com/dyllove98/archive/2013 ...
- LINQ TO SQL 常用语法
LINQ To SQL中IN的用法 IN: C# var result = from s1 in context.Customers where (new string[] { "UK& ...
- SQL-Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- oracle exp imp
oracle exp/imp
- 如何从google play下载app应用,直接下载apk
如何从google play直接下载apk by fly2004jun 2013-10-05 转载请附出处 由于某些原因,大天朝局域网访问google很多服务不能用,其中就包括google ...
- 取得select框的text
function selectInput(choose) { alert(choose.options[choose.selectedIndex].text); }