From 523ab2605add17fdaf689aea8c5800737c20b68a Mon Sep 17 00:00:00 2001
From: Alex Rakoczy <scissorjammer@gmail.com>
Date: Tue, 30 Sep 2014 11:23:34 -0400
Subject: [PATCH] Limit maxmind local subselect query to reduce cost

Since we only want to select one IP block anyway, limiting this query
reduces the query cost from (cost=22531.65..22539.67 rows=1 width=31)
to (cost=0.53..8.54 rows=1 width=31)
---
 lib/geocoder/lookups/maxmind_local.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/geocoder/lookups/maxmind_local.rb b/lib/geocoder/lookups/maxmind_local.rb
index c6fa4a06..059a19e6 100644
--- a/lib/geocoder/lookups/maxmind_local.rb
+++ b/lib/geocoder/lookups/maxmind_local.rb
@@ -36,7 +36,7 @@ module Geocoder::Lookup
         addr = IPAddr.new(query.text).to_i
         q = "SELECT l.country, l.region, l.city, l.latitude, l.longitude
           FROM maxmind_geolite_city_location l WHERE l.loc_id = (SELECT b.loc_id FROM maxmind_geolite_city_blocks b
-          WHERE b.start_ip_num <= #{addr} AND #{addr} <= b.end_ip_num)"
+          WHERE b.start_ip_num <= #{addr} AND #{addr} <= b.end_ip_num LIMIT 1)"
         format_result(q, [:country_name, :region_name, :city_name, :latitude, :longitude])
       elsif configuration[:package] == :country
         addr = IPAddr.new(query.text).to_i
-- 
GitLab