MySQL多个字段之间范围查询速度优化 全表扫描查询超30%即使begin_ip、end_ip有索引,也是不走索引的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 强制使用 `idx_geoip_org_iprange`这个索引试试 你的SQL语句,EXPLAIN SQL结果贴 出来 `idx_geoip_org_iprange` (`begin_ip`,`end_ip`), 对这样的查询根本没有作用。 这种复合索引主要是用于 `begin_ip`=xxxx and `end_ip`>... 这类的查询上。KEY `idx_geoip_org_bi` (`begin_ip`), KEY `idx_geoip_org_ei` (`end_ip`)这两个索引MYSQL仅会使用一个。 where begin_ip>=1539822726 and end_ip<=1539822726;这类语句无法优化。 其实,反倒更加慢了:mysql> select * from geoip_org_range force index(idx_uni_geoip_org_iprange) where begin_ip<=3756523522 and end_ip>=3756523522;+---------+------------+------------+--------------------+--------+------------+---------------------+| id | begin_ip | end_ip | organization | status | created_at | updated_at |+---------+------------+------------+--------------------+--------+------------+---------------------+| 6075568 | 3756523520 | 3756589054 | Bharti Airtel Ltd. | 0 | 2012-08-13 | 2012-08-13 18:30:03 |+---------+------------+------------+--------------------+--------+------------+---------------------+1 row in set (14.69 sec)mysql> explain select * from geoip_org_range force index(idx_uni_geoip_org_iprange) where begin_ip<=3756523522 and end_ip>=3756523522;+----+-------------+-----------------+-------+---------------------------+---------------------------+---------+------+---------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-----------------+-------+---------------------------+---------------------------+---------+------+---------+-------------+| 1 | SIMPLE | geoip_org_range | range | idx_uni_geoip_org_iprange | idx_uni_geoip_org_iprange | 8 | NULL | 6075562 | Using where |+----+-------------+-----------------+-------+---------------------------+---------------------------+---------+------+---------+-------------+ 试试拆成2条sql,最后再join下。 这个确实无法通过优化SQL来提高查询速度。最后我选择了对原始数据进行处理,通过对原始数据压缩,存储到文件中进行查询访问,速度极大地提高了。 mysql怎样把表的记录导出txt格式 mysql.sock 和mysqld.sock的疑问 请教字符转字段,交叉查询~ 这样的数据量mysql能否支持? Mysql中count和group by的问题 password()与password_old(),这两个函数区别是什么呢? Sql替换字段内的内容如果带特殊符号怎么办? myodbc怎么使用呀? 急救!999 高分请教mysql简单问题 Mysql 导入binary类型,被截断了 求把TXT的数据导入MYSQL的方法
结果贴 出来
KEY `idx_geoip_org_ei` (`end_ip`)
这两个索引MYSQL仅会使用一个。 where begin_ip>=1539822726 and end_ip<=1539822726;
这类语句无法优化。
+---------+------------+------------+--------------------+--------+------------+---------------------+
| id | begin_ip | end_ip | organization | status | created_at | updated_at |
+---------+------------+------------+--------------------+--------+------------+---------------------+
| 6075568 | 3756523520 | 3756589054 | Bharti Airtel Ltd. | 0 | 2012-08-13 | 2012-08-13 18:30:03 |
+---------+------------+------------+--------------------+--------+------------+---------------------+
1 row in set (14.69 sec)mysql> explain select * from geoip_org_range force index(idx_uni_geoip_org_iprange) where begin_ip<=3756523522 and end_ip>=3756523522;
+----+-------------+-----------------+-------+---------------------------+---------------------------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+-------+---------------------------+---------------------------+---------+------+---------+-------------+
| 1 | SIMPLE | geoip_org_range | range | idx_uni_geoip_org_iprange | idx_uni_geoip_org_iprange | 8 | NULL | 6075562 | Using where |
+----+-------------+-----------------+-------+---------------------------+---------------------------+---------+------+---------+-------------+
最后我选择了对原始数据进行处理,通过对原始数据压缩,存储到文件中进行查询访问,速度极大地提高了。