<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eigenjoy &#187; scalability</title>
	<atom:link href="http://eigenjoy.com/category/scalability/feed/" rel="self" type="application/rss+xml" />
	<link>http://eigenjoy.com</link>
	<description>a programming blog</description>
	<lastBuildDate>Wed, 14 Dec 2011 18:22:45 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Chaining :include&#8217;s in Rails to reduce the number of SQL queries</title>
		<link>http://eigenjoy.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/</link>
		<comments>http://eigenjoy.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 17:51:33 +0000</pubDate>
		<dc:creator>Matt Pulver</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/</guid>
		<description><![CDATA[Say you have the following data model

and you want to execute a single query that returns all the data at once within the ActiveRecord tables, with the proper rails associations between them. Wouldn&#8217;t it be nice if you could do something like

A.find&#40;:all, :include =&#62; :b =&#62; :c =&#62; :d =&#62; :e&#41; #v1

? Though this is [...]]]></description>
			<content:encoded><![CDATA[<p>Say you have the following data model</p>
<p><img src='http://www.xcombinator.com/wp-content/uploads/2008/04/associations.gif' alt='A-B-C-D-E' /></p>
<p>and you want to execute a single query that returns all the data at once within the ActiveRecord tables, with the proper rails associations between them. Wouldn&#8217;t it be nice if you could do something like</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">A.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:c</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:d</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:e</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#v1</span></pre></div></div>

<p>? Though this is not even valid ruby code, it actually comes very close to what you can do in Ruby on Rails. To get this right, let&#8217;s take a closer look at the rails associations within the class definitions:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> A <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:b</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> B <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:as</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:cs</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> C <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:b</span>
  has_one <span style="color:#ff3333; font-weight:bold;">:d</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> D <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:c</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:es</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> E <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:ds</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Let&#8217;s try the rails code again, putting an &#8217;s&#8217; after the :c and :e as required by rails in order to denote they are &#8220;many&#8221;-type associations:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">A.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:cs</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:d</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:es</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#v2</span></pre></div></div>

<p>That&#8217;s closer, but still not valid ruby code. To fix that, think of the => operator as being right-associative, and instead of putting in parentheses (), put in curly braces {} in order to create nested hashes:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">A.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:b <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:cs <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:d <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:es</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#v3</span></pre></div></div>

<p>That&#8217;s it! Looking in the logs, we see that this only produced a single query, with all the desired SQL joins:</p>
<p><code>A Load Including Associations (0.001088)   SELECT `as`.`id` AS t0_r0, `as`.`b_id` AS t0_r1, `bs`.`id` AS t1_r0, `cs`.`id` AS t2_r0, `cs`.`b_id` AS t2_r1, `cs`.`d_id` AS t2_r2, `ds`.`id` AS t3_r0, `ds`.`c_id` AS t3_r1, `es`.`id` AS t4_r0 FROM `as` LEFT OUTER JOIN `bs` ON `bs`.id = `as`.b_id LEFT OUTER JOIN `cs` ON cs.b_id = bs.id LEFT OUTER JOIN `ds` ON ds.c_id = cs.id LEFT OUTER JOIN `ds_es` ON `ds_es`.d_id = `ds`.id LEFT OUTER JOIN `es` ON `es`.id = `ds_es`.e_id</code></p>
<p>With this tool in mind, you can use this in any ActiveRecord function that accepts the :include option to reduce the number of times the rails app hits the database, and ultimately speed up your rails application.</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;title=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries&amp;notes=Say%20you%20have%20the%20following%20data%20model%0D%0A%0D%0A%0D%0A%0D%0Aand%20you%20want%20to%20execute%20a%20single%20query%20that%20returns%20all%20the%20data%20at%20once%20within%20the%20ActiveRecord%20tables%2C%20with%20the%20proper%20rails%20associations%20between%20them.%20Wouldn%27t%20it%20be%20nice%20if%20you%20could%20do%20something%20like%0D" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;title=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;t=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;title=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries&amp;annotation=Say%20you%20have%20the%20following%20data%20model%0D%0A%0D%0A%0D%0A%0D%0Aand%20you%20want%20to%20execute%20a%20single%20query%20that%20returns%20all%20the%20data%20at%20once%20within%20the%20ActiveRecord%20tables%2C%20with%20the%20proper%20rails%20associations%20between%20them.%20Wouldn%27t%20it%20be%20nice%20if%20you%20could%20do%20something%20like%0D" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;t=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

