4 ms·
Correct me if I'm wrong, but I'm pretty sure ActiveRecord will issue a separate query for each of those :include clauses.
by texel 16y ago
Correct me if I'm wrong, but I'm pretty sure ActiveRecord will issue a separate query for each of those :include clauses.
- adelevie 16y agohttp://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html http://api.rubyonrails.org/classes/ActiveRecord/Associations... > This will load all comments with a single query. This reduces the total number of queries to 3. More generally the number of queries will be 1 plus the number of associations named (except if some of the associations are polymorphic belongs_to - see below). Good call. Still, the difference between 1 and 3 queries is nothing compared to 3 and 100s (n+1 problem). I wonder what AR can't turn that into a single query.
- texel 16y agoOh you're right about that. What may not be entirely obvious though is that oftentimes 1 query can be far slower than 3 queries. In a lot of cases, that one query would have to do joins against multiple tables– I've personally seen speedups of 20x or more by just decomposing a single complex query into a few smaller and more focused queries.