<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Re: Simple RoR+MySQL optimization</title>
	<atom:link href="http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/</link>
	<description>code and content</description>
	<lastBuildDate>Thu, 02 Feb 2012 08:43:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: Chris Heald</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-19</link>
		<dc:creator>Chris Heald</dc:creator>
		<pubDate>Wed, 01 Oct 2008 16:33:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-19</guid>
		<description>JGeiger: 
 
Here&#039;s what I came up with. Not too surprising. :) 
 
&lt;pre&gt;&lt;code&gt; 
[chris@polaris benchmark]$ script/runner benchmark.rb 
find_by_sql (10000x) 
  2.430000   0.580000   3.010000 (  4.457347) 
find_by_id (10000x) 
  4.520000   0.490000   5.010000 (  6.620790) 
find with conditions (10000x) 
  3.740000   0.530000   4.270000 (  5.881615) 
&lt;/code&gt;&lt;/pre&gt; </description>
		<content:encoded><![CDATA[<p>JGeiger:</p>
<p>Here&#039;s what I came up with. Not too surprising. :)</p>
<p>&lt;pre&gt;&lt;code&gt;</p>
<p>[chris@polaris benchmark]$ script/runner benchmark.rb</p>
<p>find_by_sql (10000x)</p>
<p>  2.430000   0.580000   3.010000 (  4.457347)</p>
<p>find_by_id (10000x)</p>
<p>  4.520000   0.490000   5.010000 (  6.620790)</p>
<p>find with conditions (10000x)</p>
<p>  3.740000   0.530000   4.270000 (  5.881615)</p>
<p>&lt;/code&gt;&lt;/pre&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JGeiger</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-17</link>
		<dc:creator>JGeiger</dc:creator>
		<pubDate>Wed, 01 Oct 2008 08:54:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-17</guid>
		<description>I was wondering about what the difference would be for #1 if you replaced User.find_by_sql &quot;select * from users where id = 123&quot; or User.find_by_id 123 with 
User.find(:all, :conditions =&gt; [&quot;id = ?&quot;, 123]) 
 
It would seem that the find_by_id adds missing_method overhead, while the find(:all) should bypass that, and give you something a bit more similar to the find_by_sql. 
 
Thanks. </description>
		<content:encoded><![CDATA[<p>I was wondering about what the difference would be for #1 if you replaced User.find_by_sql &quot;select * from users where id = 123&quot; or User.find_by_id 123 with</p>
<p>User.find(:all, :conditions =&gt; [&quot;id = ?&quot;, 123])</p>
<p>It would seem that the find_by_id adds missing_method overhead, while the find(:all) should bypass that, and give you something a bit more similar to the find_by_sql.</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Heald</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-9</link>
		<dc:creator>Chris Heald</dc:creator>
		<pubDate>Wed, 01 Oct 2008 00:04:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-9</guid>
		<description>Doh! Stupid me on the quote labels there. I&#039;ve corrected it - thanks. :)

These were run against my development box, though, which does have the MySQL query cache turned off so I can more easily track down errant queries and the like.</description>
		<content:encoded><![CDATA[<p>Doh! Stupid me on the quote labels there. I&#8217;ve corrected it &#8211; thanks. :)</p>
<p>These were run against my development box, though, which does have the MySQL query cache turned off so I can more easily track down errant queries and the like.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken Miller</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-15</link>
		<dc:creator>Ken Miller</dc:creator>
		<pubDate>Tue, 30 Sep 2008 18:22:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-15</guid>
		<description>OK, that&#039;s better.  :)  Thanks for the benchmarks... </description>
		<content:encoded><![CDATA[<p>OK, that&#039;s better.  :)  Thanks for the benchmarks&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Heald</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-14</link>
		<dc:creator>Chris Heald</dc:creator>
		<pubDate>Tue, 30 Sep 2008 18:20:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-14</guid>
		<description>Generated a table with 5,000,000 rows, consisting of an id field and a value field; id field is a primary key, type INT. 
 
The benchmarking script (with corrected labels!) then produces the following: 
 
Run #1: 
&lt;pre&gt;&lt;code&gt;Without quotes (10000x): 
  0.090000   0.210000   0.300000 (  0.929264) 
With quotes (10000x): 
  0.120000   0.220000   0.340000 (  0.916973)&lt;/code&gt;&lt;/pre&gt; 
 
Run #2: 
&lt;pre&gt;&lt;code&gt;Without quotes (10000x): 
  0.120000   0.140000   0.260000 (  0.903709) 
With quotes (10000x): 
  0.140000   0.170000   0.310000 (  0.951744)&lt;/code&gt;&lt;/pre&gt; 
 
The zip code thing makes perfect sense, though - I absolutely agree that when you&#039;re querying against a string column type, make sure that you&#039;re passing a string! The example the original author had, though, was passing an ID in a query as as a string, which observationally just doesn&#039;t decrease performance to any significant degree. </description>
		<content:encoded><![CDATA[<p>Generated a table with 5,000,000 rows, consisting of an id field and a value field; id field is a primary key, type INT.</p>
<p>The benchmarking script (with corrected labels!) then produces the following:</p>
<p>Run #1:</p>
<p>&lt;pre&gt;&lt;code&gt;Without quotes (10000x):</p>
<p>  0.090000   0.210000   0.300000 (  0.929264)</p>
<p>With quotes (10000x):</p>
<p>  0.120000   0.220000   0.340000 (  0.916973)&lt;/code&gt;&lt;/pre&gt;</p>
<p>Run #2:</p>
<p>&lt;pre&gt;&lt;code&gt;Without quotes (10000x):</p>
<p>  0.120000   0.140000   0.260000 (  0.903709)</p>
<p>With quotes (10000x):</p>
<p>  0.140000   0.170000   0.310000 (  0.951744)&lt;/code&gt;&lt;/pre&gt;</p>
<p>The zip code thing makes perfect sense, though &#8211; I absolutely agree that when you&#039;re querying against a string column type, make sure that you&#039;re passing a string! The example the original author had, though, was passing an ID in a query as as a string, which observationally just doesn&#039;t decrease performance to any significant degree.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken Miller</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-13</link>
		<dc:creator>Ken Miller</dc:creator>
		<pubDate>Tue, 30 Sep 2008 18:16:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-13</guid>
		<description>Well, the more I think about it, the more I&#039;m on crack wrt the performance implications of passing strings into int columns.  But the other way around is definitely true, even though the query will work just fine.  We saw this with zipcodes:  to tell if SELECT * FROM zipcodes WHERE code = 500 will match a row with code = &#039;00500&#039; it has to actually convert the string, according to its match semantics. </description>
		<content:encoded><![CDATA[<p>Well, the more I think about it, the more I&#039;m on crack wrt the performance implications of passing strings into int columns.  But the other way around is definitely true, even though the query will work just fine.  We saw this with zipcodes:  to tell if SELECT * FROM zipcodes WHERE code = 500 will match a row with code = &#039;00500&#039; it has to actually convert the string, according to its match semantics.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken Miller</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-12</link>
		<dc:creator>Ken Miller</dc:creator>
		<pubDate>Tue, 30 Sep 2008 18:13:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-12</guid>
		<description>Actually, I should caveat that.  Passing strings to ints isn&#039;t as bad as passing ints to strings, depending on how MySQL is set up.  But we&#039;ve definitely been bitten many many times by weird, difficult-to-reproduce bugs when passing strings as ids. </description>
		<content:encoded><![CDATA[<p>Actually, I should caveat that.  Passing strings to ints isn&#039;t as bad as passing ints to strings, depending on how MySQL is set up.  But we&#039;ve definitely been bitten many many times by weird, difficult-to-reproduce bugs when passing strings as ids.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Heald</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-11</link>
		<dc:creator>Chris Heald</dc:creator>
		<pubDate>Tue, 30 Sep 2008 18:10:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-11</guid>
		<description>That&#039;s an interesting observation; I would have expected the database to perform a cast of the passed value to the expected data type of the field before passing it to the index. I&#039;ve been doing development against SQL databases for nearly a decade, and have never run into that one. 
 
I&#039;ll generate and run some tests against some tables. Is there a good reason that the database wouldn&#039;t cast the value to the column&#039;s data type? </description>
		<content:encoded><![CDATA[<p>That&#039;s an interesting observation; I would have expected the database to perform a cast of the passed value to the expected data type of the field before passing it to the index. I&#039;ve been doing development against SQL databases for nearly a decade, and have never run into that one.</p>
<p>I&#039;ll generate and run some tests against some tables. Is there a good reason that the database wouldn&#039;t cast the value to the column&#039;s data type?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken Miller</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-10</link>
		<dc:creator>Ken Miller</dc:creator>
		<pubDate>Tue, 30 Sep 2008 18:08:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-10</guid>
		<description>One more comment: the quotes/non-quotes issue is VERY real, and works both ways, strings as ints, and ints as strings.  But to really see it, you need to be working on a largish table with an index on the column in question, because what&#039;s going on is that when you pass the wrong type of data, it screws with the index utilization.  If you pass a string to an indexed int column, rather than convert the value you passed to an int, it converts EVERY VALUE IN THAT COLUMN to a string, which triggers a table scan. 
 
There are also subtle bugs that come in, such as 64-bit overflow when converting strings on 32-bit hardware, etc.  In any case, it&#039;s just a good idea to have your data types matching. </description>
		<content:encoded><![CDATA[<p>One more comment: the quotes/non-quotes issue is VERY real, and works both ways, strings as ints, and ints as strings.  But to really see it, you need to be working on a largish table with an index on the column in question, because what&#039;s going on is that when you pass the wrong type of data, it screws with the index utilization.  If you pass a string to an indexed int column, rather than convert the value you passed to an int, it converts EVERY VALUE IN THAT COLUMN to a string, which triggers a table scan.</p>
<p>There are also subtle bugs that come in, such as 64-bit overflow when converting strings on 32-bit hardware, etc.  In any case, it&#039;s just a good idea to have your data types matching.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken Miller</title>
		<link>http://www.coffeepowered.net/2008/09/30/re-simple-rormysql-optimization/comment-page-1/#comment-8</link>
		<dc:creator>Ken Miller</dc:creator>
		<pubDate>Tue, 30 Sep 2008 18:02:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.coffeepowered.net/?p=58#comment-8</guid>
		<description>Hi there, 
 
In the quotes vs. non-quotes your experimental setup is exactly wrong.  You print &quot;without quotes&quot; next to the one that has quotes and vice versa.  :)  Also, unless you&#039;ve specifically disabled it, you&#039;re likely running all your queries out of MySQL&#039;s query cache.  To disable, put /* SQL_NO_CACHE */ somewhere in the query (after restarting your db to clear any existing cache) or just set system variable query_cache_type to OFF. </description>
		<content:encoded><![CDATA[<p>Hi there,</p>
<p>In the quotes vs. non-quotes your experimental setup is exactly wrong.  You print &quot;without quotes&quot; next to the one that has quotes and vice versa.  :)  Also, unless you&#039;ve specifically disabled it, you&#039;re likely running all your queries out of MySQL&#039;s query cache.  To disable, put /* SQL_NO_CACHE */ somewhere in the query (after restarting your db to clear any existing cache) or just set system variable query_cache_type to OFF.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

