<?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>Geoff Evason &#187; optimization</title>
	<atom:link href="http://geoff.evason.name/tag/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://geoff.evason.name</link>
	<description></description>
	<lastBuildDate>Wed, 30 Nov 2011 03:41:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The (insane) Importance Of Database Indices</title>
		<link>http://geoff.evason.name/2007/12/20/the-insane-importance-of-database-indices/</link>
		<comments>http://geoff.evason.name/2007/12/20/the-insane-importance-of-database-indices/#comments</comments>
		<pubDate>Thu, 20 Dec 2007 22:41:43 +0000</pubDate>
		<dc:creator>Geoff</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://startingrails.com/2007/12/20/the-insane-importance-of-database-indices/</guid>
		<description><![CDATA[One of the great things about Rails (especially for beginners) is that you need to know very little about database.
One of the bad things about Rails (especially for beginners) is that you need to know very little about database.
Through all of the literature I read while learning rails, I only have a vague recollection of [...]]]></description>
			<content:encoded><![CDATA[<p>One of the <strong>great </strong>things about Rails (especially for beginners) is that you need to know very little about database.</p>
<p>One of the <strong>bad </strong>things about Rails (especially for beginners) is that you need to know very little about database.</p>
<p>Through all of the literature I read while learning rails, I only have a vague recollection of databases indexes being mentioned.   I started learning more about them recently because the performance of one of my apps was degrading over time, and literally, in about 2 hours, I increased the performance of my backend processing by 5x!!!</p>
<p>Intrigued?  First &#8211; some background:</p>
<p>Most tables in rails (anything that has a model really) has a field &#8216;id&#8217;.  This is the primary key of the database, and an index is created on this.  I am a database rookie, but I know that an index is just like an index in a book.  It&#8217;s extra info that the database stores so that it can find things more quickly.</p>
<p>Indices are not a concern when you get started, nor should they be.  When your table gets to 1000s of entries, then they CAN become more important.  It largely depends on how your app works.</p>
<p>Lets take a simplified example.  Lets say you have an app that has sites and pages:</p>
<pre><code class="ruby">
create_table "sites", :force =&gt; true do |t|
  t.column "permalink", :string
end

create_table "pages", :force =&gt; true do |t|
  t.column "site_id", :integer
end
</code></pre>
<p>Someone can view a site, and each site has pages:</p>
<pre><code class="ruby">
class Site &lt; ActiveRecord::Base
  has_many :pages
end

</code></pre>
<p>Because you want pretty urls you want someone to be able to go to www.myapp.com/&lt;site.name&gt;/&lt;page.id&gt;. The first thing you need to do that is</p>
<pre><code class="ruby">
@site = Site.find_by_permalink(params[:id])
</code></pre>
<p>Of course, you are also going to need to have navigation for that site, so while rendering you have something like this</p>
<pre><code class="ruby">
&lt;div id="nav"&gt;
  &lt;% for page in @site.pages %&gt;
     &lt;%# (do something with the page here) %&gt;
  &lt;% end %&gt;
&lt;/div&gt;
</code></pre>
<p>So what happens, when your someone tries to view a site?</p>
<p>Step 1: find_by_permalink:  The database has to open every single &#8217;site&#8217; and check if the permalink matches.</p>
<p>Step 2: @site.pages:  The database has to open every single page and check if the event_id field matches the id of the event that it is looking for.</p>
<p>When you have small numbers of rows, this isn&#8217;t a big problem. When you get 1000s of rows, looking through every site and every page gets slow, quickly.</p>
<p>So, the solution is to add an index.   You add indices in Rails using migrations.  You can create a migration like this:</p>
<pre><code class="ruby">
class AddIndices &lt; ActiveRecord::Migration
  def self.up
    add_index :sites, :permalink
    add_index :pages, :site_id
  end

  def self.down
    remove_index :sites, :permalink
    remove_index :pages, :site_id
  end
end</code></pre>
<p>When you add those 2 indices, the database can now much more quickly find and retrieve the single Site object by permalink, and find and retrieve only the required Page objects rather than having to search the entire database for a match.</p>
<p>Doing this got my a 5x improvement in the speed of some of my more common actions, and literally took 2 hours.</p>
<p>You do need to be careful.  Adding indices <strong>can slow down your performance </strong>because every time you insert, update, or delete rows, the database must also alter the index.</p>
<p>Some useful links:</p>
<p><a title="rails database index" href="http://therailsway.com/2006/11/21/tracks-part-4">http://therailsway.com/2006/11/21/tracks-part-4</a></p>
<p><a title="rails database index info" href="http://weblog.jamisbuck.org/2006/10/23/indexing-for-db-performance">http://weblog.jamisbuck.org/2006/10/23/indexing-for-db-performance</a></p>
<p><a href="http://www.websitedatabases.com/database-index.html ">http://www.websitedatabases.com/database-index.html </a></p>
<p>There are probably good ways of benchmarking this.  After testing locally, I copied my live data to my staging area, added the indices, ran a few actions, and compared the logs from before/after the migration.  In my case, some actions got a bitter slower, but those were uncommon ones.  My most common actions got 5x faster!!  Have a look at the following graph.  The red lines show the % of time for an action after adding the indices.  Above 100% means the action got slower.  Below means it got faster.  You an see most actions got a lot faster!</p>
<p><img src="http://geoff.evason.name/wp-content/uploads/2007/12/performance.jpg" alt="Effect of Database Index" /></p>
]]></content:encoded>
			<wfw:commentRss>http://geoff.evason.name/2007/12/20/the-insane-importance-of-database-indices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>YSlow On Rails</title>
		<link>http://geoff.evason.name/2007/10/17/yslow-on-rails/</link>
		<comments>http://geoff.evason.name/2007/10/17/yslow-on-rails/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 03:04:31 +0000</pubDate>
		<dc:creator>Geoff</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://startingrails.com/2007/10/17/yslow-on-rails/</guid>
		<description><![CDATA[I gave a talk recently on how to improve the front end performance of a rails app using YSlow as a tool to measure said performance.

]]></description>
			<content:encoded><![CDATA[<p>I gave a talk recently on how to improve the front end performance of a rails app using YSlow as a tool to measure said performance.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="437" height="370" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="id" value="viddler" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="src" value="http://www.viddler.com/player/75870d40/" /><embed id="viddler" type="application/x-shockwave-flash" width="437" height="370" src="http://www.viddler.com/player/75870d40/" allowfullscreen="true" allowscriptaccess="always"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://geoff.evason.name/2007/10/17/yslow-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 Best Tools For Testing, Debugging, &amp; Optimizing Websites</title>
		<link>http://geoff.evason.name/2007/09/23/10-best-tools-for-testing-debugging-optimizing-web-apps/</link>
		<comments>http://geoff.evason.name/2007/09/23/10-best-tools-for-testing-debugging-optimizing-web-apps/#comments</comments>
		<pubDate>Sun, 23 Sep 2007 07:47:44 +0000</pubDate>
		<dc:creator>Geoff</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://startingrails.com/?p=13</guid>
		<description><![CDATA[I&#8217;ve learned a lot about web apps in recent months, so I thought I summarize what I have found to be the most useful resources in terms of helping me do things well.
1. Firebug: http://www.getfirebug.com Firebug is a plugin for firefox that, well, lets you see any detail about a web page that you could [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve learned a lot about web apps in recent months, so I thought I summarize what I have found to be the most useful resources in terms of helping me do things well.</p>
<p><strong>1. Firebug: </strong><a href="http://www.getfirebug.com/" target="_blank">http://www.getfirebug.com</a> Firebug is a plugin for firefox that, well, lets you see any detail about a web page that you could conceivably what to see. I&#8217;ve only ever heard good things about, and the accolades are very well deserved.  So far I&#8217;ve used it for:</p>
<ol>
<li>Analyzing the DOM</li>
<li>Stepping through (and setting break points) in Javascript</li>
<li>Viewing Javascript errors</li>
<li>Viewing Styles</li>
</ol>
<p>To be honest &#8211; I think I&#8217;ve only just scratched the surface.</p>
<p><strong>2. Firefox Web Developer Toolbar</strong> <a href="https://addons.mozilla.org/en-US/firefox/addon/60" target="_blank">https://addons.mozilla.org/en-US/firefox/addon/60</a> There may be some overlap between this and firebug, but I use both.  I find this particularly good for debugging styling.  &#8216;View Style Information&#8217; is killer.  It is also very useful for testing your site with javascript or cookies disabled.</p>
<p><strong>3. YSlow</strong> <a href="http://developer.yahoo.com/yslow/" target="_blank">http://developer.yahoo.com/yslow/</a> This is actually a plugin for firebug (yeah &#8211; a plugin for a plugin).  It analyzes a page using 13 metrics (all with very details descriptions that are linked to), and gives a grade for each one, as well as an overall grade.  I took my score from 33 to 60+ but my site is so much more responsive than it used to be.  The biggest gain was simply by putting all the javascript at the end, so the page visually loads so much faster.  I&#8217;m moving to a dedicated server at present, and will then be able to increase the grade even more.</p>
<p><strong>4. Total Validator</strong> <a href="https://addons.mozilla.org/en-US/firefox/addon/2318" target="_blank">https://addons.mozilla.org/en-US/firefox/addon/2318</a> This basically just redirects you to a online html validator, though the pro version does it locally I believe.  As I understand, valid HTML is good for both SEO &amp; cross browser compatability (well &#8211; except for IE).</p>
<p><strong>5. Selenium</strong> <a href="http://www.openqa.org/selenium/">http://www.openqa.org/selenium/</a> Selenium uses javascript to test drive your app.  I wrote about it before.  Backend testing is easy in rails, but there is no way to test the user interface, especially for javascript heavy sights. Selenium is the answer.  With the IDE you can even record actions and run them as test later <img src='http://geoff.evason.name/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><strong>6. IE Developer Toolbar</strong> <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=E59C3964-672D-4511-BB3E-2D5E1DB91038&amp;displaylang=en" target="_blank">(ie toolbar download)</a> This is basically a combination of firebug &amp; the firefox web developer toolbar.  It&#8217;s pretty good, and I&#8217;ve used it several times to track what was happening in IE.  It only works with IE7 though &#8211; which is too bad since most of my problems are with IE6&#8230;</p>
<p><strong>7. ScreenGrab</strong> <a href="http://www.screengrab.org/" target="_blank">http://www.screengrab.org/</a> This firefox plugin lets you render a page (visible portion or whole thing) to an image file.  One note &#8211; it allows you to save with a .jpg extension, but really saves in .png format.  This caused me problems when I was trying to do something with an image that needed to really be in .jpg format&#8230;</p>
<p><strong>8. Quick Locale Switcher</strong> <a href="https://addons.mozilla.org/en-US/firefox/addon/1333" target="_blank">https://addons.mozilla.org/en-US/firefox/addon/1333</a> This is ideal for testing an app with localization.  I&#8217;ve only played around with it quickly as I&#8217;m not doing localization yet, but it will come in very handy when I do.</p>
<p><strong>9. Site Report Card</strong> <a href="http://www.sitereportcard.com" target="_blank">http://www.sitereportcard.com </a> This is an online tool that checks a number of things on your site, such as inclusion in search engines, html validity, spelling, links, etc.</p>
<p><strong>10. Love</strong> <a href="http://www.spreadfirefox.com/node/28395" target="_blank">Love</a> Okay &#8211; so I really only have 9 tools &#8211; but 10 just seems like a better number for a title&#8230;  Love is good though. Spread the love and all will be well&#8230;</p>
<p>So there&#8217;s my opinion of the top 10 tools for testing, debugging, and optimizing web apps.  most of them are firefox add-ons, but I would recommend against getting too dependent on firefox. The &lt;problem&gt; with firefox is the lack of bugs&#8230;  Rotate browsers when developing&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://geoff.evason.name/2007/09/23/10-best-tools-for-testing-debugging-optimizing-web-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

