<?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>Ruby on Rails Outsourcing &#187; plugin</title>
	<atom:link href="http://www.ruby-on-rails-outsourcing.com/tag/plugin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ruby-on-rails-outsourcing.com</link>
	<description>Kirana Tama - Rails outsourcing company that helps small businesses minimize their costs</description>
	<lastBuildDate>Wed, 28 Jul 2010 05:58:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Bullet to detect n+1 queries problem</title>
		<link>http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/</link>
		<comments>http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 03:29:06 +0000</pubDate>
		<dc:creator>William Notowidagdo</dc:creator>
				<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.ruby-on-rails-outsourcing.com/?p=103</guid>
		<description><![CDATA[Probably you already know about n+1 queries problem, read this post if you want to learn how to detect those kind of queries.]]></description>
			<content:encoded><![CDATA[<p>Probably you already know about n+1 queries problem, read this post if you want to learn how to detect those kind of queries.<span id="more-103"></span></p>
<p>For example:</p>
<pre class="brush: rails">
class User &lt; ActiveRecord::Base
  has_many :items
end

class Item &lt; ActiveRecord::Base
  belongs_to :user
end
</pre>
<p>If we have 2 users, and several items we have controller like this:</p>
<pre class="brush: rails">
def index
  @users = User.find(:all)
end
</pre>
<p>and in the view we have something like this:</p>
<pre class="brush: rails">
&lt;% @users.each do |user| %&gt;
    &lt;%= user.items.collect(&amp;:name) %&gt;
&lt;% end %&gt;
</pre>
<p>If we watch the queries on the log, we should see something like this :</p>
<pre class="brush: sql">
SELECT * FROM "users"
SELECT * FROM "items" WHERE ("items".user_id = 1)
SELECT * FROM "items" WHERE ("items".user_id = 2)
</pre>
<p>if we have 1000 items, that means we are doing 1000+1 queries!<br />
Off course we can fix it like this (eager loading):</p>
<pre class="brush: rails">
def index
  @users = User.find(:all, :include =&gt; :items)
end
</pre>
<p>And now we only have two queries.</p>
<p>Detecting it in our application is another matter. If we have huge project, detecting it can be hard work and time wasting. I found a cool plugin/gem that help detecting n+1 queries problem, called <strong><a href="http://www.huangzhimin.com/projects/4-bullet">Bullet</a></strong>. You can install it as a gem:</p>
<pre class="brush: bash">
sudo gem install flyerhzm-bullet --source http://gems.github.com
</pre>
<p>or as a plugin</p>
<pre class="brush: bash">
script/plugin install git://github.com/flyerhzm/bullet.git
</pre>
<p>or you can build the gem right from the source from:</p>
<pre class="brush: bash">
git clone git://github.com/flyerhzm/bullet.git
cd bullet
gem build bullet.gemspec
sudo gem install bullet --local
</pre>
<p>If you are using it as a gem, don&#8217;t forget to include this in your environment.rb:</p>
<pre class="brush: rails">
config.gem 'flyerhzm-bullet', :lib =&gt; 'bullet', :source =&gt; 'http://gems.github.com'
</pre>
<p>include this in development.rb, and remember not to use bullet on production:</p>
<pre class="brush: bash">
config.after_initialize do
  Bullet.enable = true  
  Bullet::Association.alert = true
  Bullet::Association.bullet_logger = true
  Bullet::Association.rails_logger = false
end
</pre>
<p>You want to make two profiles in your Firefox, one for regular browsing, and one without caching enabled since sometimes it is not working in regular profile. So disable caching on your browser if you want to see pop-up box if we are doing n+1 queries. Also Bullet detect eager loading that is not used. I attached a screenshot so you can see it in action:</p>
<p style="text-align: center;"><img class=" aligncenter" title="Bullet in action" src="http://img697.imageshack.us/img697/471/snapshot3l.png" alt="Bullet in action" width="489" height="215" /></p>
</div>
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul class="socials"><li class="sexy-delicious"><a href="http://del.icio.us/post?url=http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/&amp;title=Using+Bullet+to+detect+n%2B1+queries+problem" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li class="sexy-digg"><a href="http://digg.com/submit?phase=2&amp;url=http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/&amp;title=Using+Bullet+to+detect+n%2B1+queries+problem" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li class="sexy-stumbleupon"><a href="http://www.stumbleupon.com/submit?url=http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/&amp;title=Using+Bullet+to+detect+n%2B1+queries+problem" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li class="sexy-technorati"><a href="http://technorati.com/faves?add=http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a></li><li class="sexy-facebook"><a href="http://www.facebook.com/share.php?u=http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/&amp;t=Using+Bullet+to+detect+n%2B1+queries+problem" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li class="sexy-twitter"><a href="http://twitter.com/home?status=Using+Bullet+to+detect+n%2B1+queries+problem+-+http://b2l.me/ah3pd+(via+@ptwgs)" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li class="sexy-mail"><a href="mailto:?subject=%22Using%20Bullet%20to%20detect%20n%2B1%20queries%20problem%22&amp;body=I%20thought%20this%20article%20might%20interest%20you.%0A%0A%22Probably%20you%20already%20know%20about%20n%2B1%20queries%20problem%2C%20read%20this%20post%20if%20you%20want%20to%20learn%20how%20to%20detect%20those%20kind%20of%20queries.%0D%0A%0D%0AFor%20example%3A%0D%0A%0D%0Aclass%20User%20%26lt%3B%20ActiveRecord%3A%3ABase%0D%0A%20%20has_many%20%3Aitems%0D%0Aend%0D%0A%0D%0Aclass%20Item%20%26lt%3B%20ActiveRecord%3A%3ABase%0D%0A%20%20belongs_to%20%3Auser%0D%0Aend%0D%0A%0D%0AIf%20we%20have%202%20users%2C%20and%20severa%22%0A%0AYou%20can%20read%20the%20full%20article%20here%3A%20http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a></li><li class="sexy-comfeed"><a href="http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li><li class="sexy-linkedin"><a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/&amp;title=Using+Bullet+to+detect+n%2B1+queries+problem&amp;summary=Probably%20you%20already%20know%20about%20n%2B1%20queries%20problem%2C%20read%20this%20post%20if%20you%20want%20to%20learn%20how%20to%20detect%20those%20kind%20of%20queries.%0D%0A%0D%0AFor%20example%3A%0D%0A%0D%0Aclass%20User%20%26lt%3B%20ActiveRecord%3A%3ABase%0D%0A%20%20has_many%20%3Aitems%0D%0Aend%0D%0A%0D%0Aclass%20Item%20%26lt%3B%20ActiveRecord%3A%3ABase%0D%0A%20%20belongs_to%20%3Auser%0D%0Aend%0D%0A%0D%0AIf%20we%20have%202%20users%2C%20and%20severa&amp;source=Ruby on Rails Outsourcing" rel="nofollow" class="external" title="Share this on Linkedin">Share this on Linkedin</a></li></ul><div style="clear:both;"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ruby-on-rails-outsourcing.com/2009/11/18/using-bullet-to-detect-n1-queries-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
