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
随机推荐
- php读取memcache二进制数据
memcache作为一个数据中间层,经常用来做数据交换. 比如在某个系统内部我们规定如下的用户状态的信息,每个用户只需要存续52个字节. Key state#ID 如”state#10888” Val ...
- Python Socket Programming
本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...
- PreparedStatement可以有效地防止sql被注入
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import jav ...
- Top 100 English Verbs
accept allow ask believe borrow break bring buy can/be able cancel change clean comb complain cough ...
- Android 将从网络获取的数据缓存到私有文件
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- 技巧:Linux 动态库与静态库制作及使用详解
技巧:Linux 动态库与静态库制作及使用详解 标准库的三种连接方式及静态库制作与使用方法 Linux 应用开发通常要考虑三个问题,即:1)在 Linux 应用程序开发过程中遇到过标准库链接在不同 L ...
- c# 实现文件拖入和拖出(拖拽)
摘自:http://www.cnblogs.com/eaglet/archive/2009/01/06/1370149.html C# WinForm下一步一步实现文件的拖入和拖出 作者:Eaglet ...
- HDU_2035——求A^B的最后三位数
Problem Description 求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方” Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1< ...
- Codeforces Round #203 - D. Looking for Owls
D. Looking for Owls Emperor Palpatine loves owls very much. The emperor has some blueprints with the ...
- C#request 请求响应
/// <summary> /// 提交POST请求 /// </summary> /// <param name="url">提交地址< ...