title: Batch geo locate millions of IPs
body: <p>I got 2 million IP addresses and 25 million IP ranges with Start IP, End IP and Geo-Locations stored in PostgreSQL. Is there an efficient way to look up the geo-locations of those 2 million IPs from the 25 million database? What I did was to compare whether an IP address falls in between the Start IP and End IP and look up the corresponding location. However this seems taking forever. Presumably this is more like looking up a bunch of integers from a set of ranges such like searching {7, 13, 31, 42} from:</p>
  
  <pre><code>Start End Loc
  1     10  US
  11    20  US
  21    26  CN
  29    32  SE
  33    45  CA
  </code></pre>
  
  <p>and returning:</p>
  
  <pre><code>7  US
  13 US
  31 SE
  42 CA
  </code></pre>
  
  <p>Please note that the ranges may not be necessarily connected and the size may not be the same. Thank you!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As a concrete example, here's the data I'm dealing with:</p>
  
  <pre><code>     start_ip     |      end_ip      | country |  region   |   city    | 
  ------------------+------------------+---------+-----------+-----------+-
   1.33.254.73/32   | 1.33.254.73/32   | jpn     | 33        | kurashiki | 
   1.39.1.0/32      | 1.39.4.255/32    | ind     | mh        | mumbai    | 
   1.40.144.0/32    | 1.40.145.255/32  | aus     | ns        | fairfield | 
   1.40.235.0/32    | 1.40.242.255/32  | aus     | ns        | sydney    | 
   1.44.28.0/32     | 1.44.29.255/32   | aus     | vi        | melbourne | 
   1.44.82.0/32     | 1.44.83.255/32   | aus     | vi        | melbourne | 
   1.44.92.0/32     | 1.44.93.255/32   | aus     | vi        | melbourne | 
   1.44.128.0/32    | 1.44.129.255/32  | aus     | vi        | melbourne | 
   1.44.220.0/32    | 1.44.221.255/32  | aus     | vi        | melbourne | 
   ......
   ......
  </code></pre>
  
  <p>And the queries are something like:</p>
  
  <pre><code> 75.149.219.61/32
   68.239.61.29/32
   96.41.50.165/32
   183.62.126.7/32
   ......
  </code></pre>