S :has_many F; F :belongs_to P; S :has_many P :through F.
Query looks like this:
S.find_all_by_key([array of keys], :include => [:F, :P])
This produces the correct and expected result, but very slowly. On the
SELECT * FROM S WHERE KEY IN (keys)
SELECT * FROM F WHERE S_ID IN (S.ids from first query)
SELECT * FROM F WHERE S_ID IN (S.ids from first query) [same query
SELECT * FROM P WHERE ID IN (ids from second query)
At this point all the required data has been retrieved and Rails is
looking good. Time to quit. However:
SELECT * FROM P WHERE ID = first id from second query
SELECT * FROM P WHERE ID = second id from second query
... [typically about 50-200 queries, one query per P.ID]
Q1. Why so many extraneous queries?
Q2. Is there an easy fix, or is this just too hard for Rails?
Q3. In general, is it possible to control eager loading at a per-query