GeoIP table join with table of IP's in MySQL

前端 未结 4 1766
终归单人心
终归单人心 2021-01-02 23:38

I am having a issue finding a fast way of joining the tables looking like that:

mysql> explain geo_ip;
+--------------+------------------+------+-----+---         


        
4条回答
  •  抹茶落季
    2021-01-02 23:41

    I found a easy way. I noticed that all first ip in the group % 256 = 0, so we can add a ip_index table

    CREATE TABLE `t_map_geo_range` (
      `_ip` int(10) unsigned NOT NULL,
      `_ipStart` int(10) unsigned NOT NULL,
      PRIMARY KEY (`_ip`)
    ) ENGINE=MyISAM
    

    How to fill the index table

    FOR_EACH(Every row of ip_geo)
    {
        FOR(Every ip FROM ipGroupStart/256 to ipGroupEnd/256)
        {
            INSERT INTO ip_geo_index(ip, ipGroupStart);
        }
    }
    

    How to use:

    SELECT * FROM YOUR_TABLE AS A
    LEFT JOIN ip_geo_index AS B ON B._ip = A._ip DIV 256
    LEFT JOIN ip_geo AS C ON C.ipStart = B.ipStart;
    

    More than 1000 times Faster.

提交回复
热议问题