全表扫描查询超30%即使begin_ip、end_ip有索引,也是不走索引的。

解决方案 »

  1.   

    强制使用 `idx_geoip_org_iprange`这个索引试试
      

  2.   

    你的SQL语句,EXPLAIN SQL
    结果贴 出来
      

  3.   

    `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;
    这类语句无法优化。
      

  4.   

    其实,反倒更加慢了: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 |
    +----+-------------+-----------------+-------+---------------------------+---------------------------+---------+------+---------+-------------+
      

  5.   

    试试拆成2条sql,最后再join下。
      

  6.   

    这个确实无法通过优化SQL来提高查询速度。
    最后我选择了对原始数据进行处理,通过对原始数据压缩,存储到文件中进行查询访问,速度极大地提高了。