I have two model objects:
Events have Venues. Venues can have 1..* events.
Venues have a location, a lat and long, which I use with the Geokit Rails plugin. Here's what these models look like in Rails:
class Event < ActiveRecord::Base
belongs_to :venue
acts_as_mappable :through => :venue
end
and
class Venue < ActiveRecord::Base
has_many :events
acts_as_mappable
end
Very simple! Now, I'd like to run a query, I'd like to find all the events within a few miles of a certain area, looking at the Geokit API, I see that I can use mappable with a :through association on Event, and should be able to call geokit finders on Event! Excellent (so you can see above, I've added acts_as_mappable :through to the Event).
Here's my ActiveRecord query:
Event.find(:all, :origin => [lat, lng], :select => 'title', :within => 5, :limit => 10)
Here's the SQL generated:
SELECT `events`.`id` AS t0_r0, `events`.`title` AS t0_r1, `events`.`description` AS t0_r2,
`events`.`created_at` AS t0_r3, `events`.`updated_at` AS t0_r4, `events`.`venue_id` AS
t0_r5, `events`.`event_detail_id` AS t0_r6, `events`.`event_detail_type` AS t0_r7,
`venues`.`id` AS t1_r0, `venues`.`name` AS t1_r1, `venues`.`lat` AS t1_r2, `venues`.`lng`
AS t1_r3, `venues`.`created_at` AS t1_r4, `venues`.`updated_at` AS t1_r5 FROM `events` LEFT
OUTER JOIN `venues` ON `venues`.id = `events`.venue_id WHERE
(((venues.lat>51.4634898826039 AND venues.lat>51.5533406301823 AND venues.lng>-
0.197713629915149 AND venues.lng<-0.053351862714855)) AND
((ACOS(least(1,COS(0.898991438708539)*COS(-0.00219095974226756)*COS(RADIANS(venues.lat))*COS(RADIANS(venues.lng))
COS(0.898991438708539)*SIN(-
0.00219095974226756)*COS(RADIANS(venues.lat))*SIN(RADIANS(venues.lng))
SIN(0.898991438708539)*SIN(RADIANS(venues.lat))))*6376.77271)
<= 5)) LIMIT 10
Ouch. Well, this takes quite some time to run, and it's not just the math, I think there's a significant problem here with performance, this takes 2 seconds to run on my desktop! But getting down to it, here is my complaint:
Furthermore, this query, when run against the Venues alone (i.e. doing away with the JOIN) is executed in less than 10ms. So is there a way to do this Venues search first, and THEN do the join against the Events? I think the join must be being done on the two tables as a whole, and only then the geocoding part comes in and slims the dataset down.
Any help in tightening up this issue would be much appreciated (this is a non-commercial project). I feel that the model is very simple I should be able to do this without too much fuss?
Thanks in advance!
(Edit: In an early version of this post I was stupid, Limit worked fine, my view was broken, edited to take that part out.)
It looks like acts_as_mappable is using the eager loading functionality of Rails Active Record. Once you start eager loading any :select argument is ignored and you cannot specify particular columns from a table/model. In your case i think this is happening because of the :through=>:venue argument to acts_as_mappable. Can you make it work without the through argument? You can always poke around the source code as well to see if there are any other adjustments you can make.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With