# Triple join asking for trouble?

**URL:** <https://www.ruby-forum.com/t/triple-join-asking-for-trouble/163375>\
**Category:** Rails\
**Created:** [March 19, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/triple-join-asking-for-trouble/163375 "2009-03-19T17:45:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [March 19, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/triple-join-asking-for-trouble/163375/1 "2009-03-19T17:45:48Z")

</div>

Model.scoped\_by\_body\_type\_id  
( event.body\_type\_ids ).scoped\_by\_skin\_color\_id  
( event.skin\_color\_ids ).find( :all,  
:conditions =\> [‘weight\_pounds \<= ? AND total\_height\_inches \>= ?  
AND minimum\_pay \<= ? AND job\_types.id = ? AND regions.id = ? AND  
languages.id in (?)’,  
event.maximum\_weight\_in\_pounds,  
event.total\_minimum\_height\_inches,  
event.payment\_per\_head,  
event.job\_type\_id,  
event.region\_id,  
event.language\_ids ],  
:joins =\> [:job\_types, :regions, :languages])

Is this triple join asking for trouble? I mean is my server going to  
hate me? language, region and job\_types are going to have less than  
10 row entries, but job\_type\_preferences, language\_preferences and  
region\_preferences will scale linearly with the number of users…

Model Load (0.7ms) SELECT `models`.\* FROM `models` INNER JOIN  
`job_type_preferences` ON (`models`.`id` =  
`job_type_preferences`.`model_id`) INNER JOIN `job_types` ON  
(`job_types`.`id` = `job_type_preferences`.`job_type_id`) INNER JOIN  
`region_preferences` ON (`models`.`id` =  
`region_preferences`.`model_id`) INNER JOIN `regions` ON  
(`regions`.`id` = `region_preferences`.`region_id`) INNER JOIN  
`language_preferences` ON (`models`.`id` =  
`language_preferences`.`model_id`) INNER JOIN `languages` ON  
(`languages`.`id` = `language_preferences`.`language_id`) WHERE  
(weight\_pounds \<= NULL AND total\_height\_inches \>= 60 AND minimum\_pay  
\<= 2000 AND job\_types.id = 4 AND regions.id = 1 AND languages.id in  
(NULL)) AND ((`models`.`body_type_id` IN (4) AND  
`models`.`skin_color_id` IN (4)))

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [March 19, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/triple-join-asking-for-trouble/163375/2 "2009-03-19T18:01:36Z")

</div>

On Mar 19, 4:45 pm, David B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this triple join asking for trouble? I mean is my server going to  
> hate me? language, region and job\_types are going to have less than  
> 10 row entries, but job\_type\_preferences, language\_preferences and  
> region\_preferences will scale linearly with the number of users…

In the absence of all other things they fewer joins the better but if  
they’re needed to do the job then that’s ok. Just be sure that you  
have indexes on those columns you join on and keep an eye on the  
explain output for that query.

Fred
