<?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"
	>
<channel>
	<title>Comments on: Subqueries and SQL Syntax Sugar Diet</title>
	<atom:link href="http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/</link>
	<description>A goal is a dream with a deadline.</description>
	<pubDate>Fri, 21 Nov 2008 01:16:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Ilya Grigorik</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-117986</link>
		<dc:creator>Ilya Grigorik</dc:creator>
		<pubDate>Tue, 26 Aug 2008 12:39:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-117986</guid>
		<description>Lynton, I think this may have been a transitional bug in one of the point releases. I've since tested this same scenario on newer versions (didn't bother reverting back to old as control), and the problem seems to have gone away.</description>
		<content:encoded><![CDATA[<p>Lynton, I think this may have been a transitional bug in one of the point releases. I&#8217;ve since tested this same scenario on newer versions (didn&#8217;t bother reverting back to old as control), and the problem seems to have gone away.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lynton Morgan</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-117741</link>
		<dc:creator>Lynton Morgan</dc:creator>
		<pubDate>Mon, 25 Aug 2008 23:33:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-117741</guid>
		<description>Did anyone confirm if these wild differences where down to storage engines or something else? No one who got the opposite result confirmed they were using Innodb</description>
		<content:encoded><![CDATA[<p>Did anyone confirm if these wild differences where down to storage engines or something else? No one who got the opposite result confirmed they were using Innodb</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ilya Grigorik</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-77422</link>
		<dc:creator>Ilya Grigorik</dc:creator>
		<pubDate>Tue, 23 Oct 2007 04:06:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-77422</guid>
		<description>Ah, that could explain it! Thanks aleco, very interesting read.</description>
		<content:encoded><![CDATA[<p>Ah, that could explain it! Thanks aleco, very interesting read.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aleco</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-77254</link>
		<dc:creator>aleco</dc:creator>
		<pubDate>Mon, 22 Oct 2007 14:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-77254</guid>
		<description>It seems as if this is an example of mysql's improper handling of optimization (seems as if 5.2 will fix it). Basically what happens is that MySQL will execute the outer statement for each item in the IN() list, therefor looping through your table 500 times instead of aggregating the 500 posts first. Plus, I think "IN" can't always make use of the index, therefor adding dramatic slowdown.

There are some details regarding the optimization of IN/ANY queries here: http://dev.mysql.com/doc/refman/5.0/en/in-subquery-optimization.html</description>
		<content:encoded><![CDATA[<p>It seems as if this is an example of mysql&#8217;s improper handling of optimization (seems as if 5.2 will fix it). Basically what happens is that MySQL will execute the outer statement for each item in the IN() list, therefor looping through your table 500 times instead of aggregating the 500 posts first. Plus, I think &#8220;IN&#8221; can&#8217;t always make use of the index, therefor adding dramatic slowdown.</p>
<p>There are some details regarding the optimization of IN/ANY queries here: <a href="http://dev.mysql.com/doc/refman/5.0/en/in-subquery-optimization.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.0/en/in-subquery-optimization.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ilya Grigorik</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-71057</link>
		<dc:creator>Ilya Grigorik</dc:creator>
		<pubDate>Sat, 22 Sep 2007 21:05:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-71057</guid>
		<description>Fred, interesting results, thanks for sharing. It appears that the query optimizer is all over the map on different platforms / versions of MySQL.</description>
		<content:encoded><![CDATA[<p>Fred, interesting results, thanks for sharing. It appears that the query optimizer is all over the map on different platforms / versions of MySQL.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fred</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-70784</link>
		<dc:creator>Fred</dc:creator>
		<pubDate>Fri, 21 Sep 2007 12:39:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-70784</guid>
		<description>forgot to format the code
here is the code I used

&lt;code&gt;
def time_query(&#038;block)
  start = Time.now
  block.call
  puts "#{Time.now - start} seconds \n"
  sleep 1
end
 
ids = []
5000.times do &#124;t&#124;
  t=t+1
  ids &lt;&lt; t
end
   
# Syntax sugar galore :
time_query {

	result = User.find_by_sql("SELECT * FROM users WHERE id in (#{ids.join(',')})")
}


# SQL on a diet: vanilla UNION way
time_query {
	sql = ids.collect { &#124;w&#124; "(SELECT * FROM users WHERE id = #{w})" }
	sql = sql.join(" UNION ")
	result = User.find_by_sql(sql)
}

&lt;/code&gt;

Cheers :)</description>
		<content:encoded><![CDATA[<p>forgot to format the code<br />
here is the code I used</p>
<p><code><br />
def time_query(&#038;block)<br />
  start = Time.now<br />
  block.call<br />
  puts "#{Time.now - start} seconds \n"<br />
  sleep 1<br />
end</p>
<p>ids = []<br />
5000.times do |t|<br />
  t=t+1<br />
  ids < < t<br />
end</p>
<p># Syntax sugar galore :<br />
time_query {</p>
<p>	result = User.find_by_sql("SELECT * FROM users WHERE id in (#{ids.join(',')})")<br />
}</p>
<p># SQL on a diet: vanilla UNION way<br />
time_query {<br />
	sql = ids.collect { |w| "(SELECT * FROM users WHERE id = #{w})" }<br />
	sql = sql.join(" UNION ")<br />
	result = User.find_by_sql(sql)<br />
}</p>
<p></code></p>
<p>Cheers :)</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fred</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-70781</link>
		<dc:creator>Fred</dc:creator>
		<pubDate>Fri, 21 Sep 2007 12:34:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-70781</guid>
		<description>I run these tests on my macbook, and the results were completely different.. 

The union query was actually 5 to 10 times *slower*

I made it to do with 5000 records:


def time_query(&#38;block)
  start = Time.now
  block.call
  puts "#{Time.now - start} seconds \n"
  sleep 1
end
 
ids = []
5000.times do &#124;t&#124;
  t=t+1
  ids 
RESULTS in Macbook:
mysql  Ver 14.12 Distrib 5.0.41, for apple-darwin8.5.1 (i686) using readline 5.0

Using IN:
0.877374 seconds 

Using UNION:
8.407131 seconds 

RESULTS in Gentoo Linux Server:
mysql  Ver 14.12 Distrib 5.0.44, for pc-linux-gnu (i686) using readline 5.2

Using IN:
1.158719 seconds 

USING UNION
7.831463 seconds 
it's very weird to have these results.

I Also tunned my.conf to have higher tunned values.
Maybe that could be the reason.</description>
		<content:encoded><![CDATA[<p>I run these tests on my macbook, and the results were completely different.. </p>
<p>The union query was actually 5 to 10 times *slower*</p>
<p>I made it to do with 5000 records:</p>
<p>def time_query(&amp;block)<br />
  start = Time.now<br />
  block.call<br />
  puts &#8220;#{Time.now - start} seconds \n&#8221;<br />
  sleep 1<br />
end</p>
<p>ids = []<br />
5000.times do |t|<br />
  t=t+1<br />
  ids<br />
RESULTS in Macbook:<br />
mysql  Ver 14.12 Distrib 5.0.41, for apple-darwin8.5.1 (i686) using readline 5.0</p>
<p>Using IN:<br />
0.877374 seconds </p>
<p>Using UNION:<br />
8.407131 seconds </p>
<p>RESULTS in Gentoo Linux Server:<br />
mysql  Ver 14.12 Distrib 5.0.44, for pc-linux-gnu (i686) using readline 5.2</p>
<p>Using IN:<br />
1.158719 seconds </p>
<p>USING UNION<br />
7.831463 seconds<br />
it&#8217;s very weird to have these results.</p>
<p>I Also tunned my.conf to have higher tunned values.<br />
Maybe that could be the reason.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Morning Brew #73</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-68046</link>
		<dc:creator>Morning Brew #73</dc:creator>
		<pubDate>Mon, 10 Sep 2007 11:01:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-68046</guid>
		<description>[...] Subqueries and SQL Syntax Sugar Diet - unexpected SQL optimization. [...]</description>
		<content:encoded><![CDATA[<p>[...] Subqueries and SQL Syntax Sugar Diet - unexpected SQL optimization. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ilya Grigorik</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-67360</link>
		<dc:creator>Ilya Grigorik</dc:creator>
		<pubDate>Fri, 07 Sep 2007 22:02:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-67360</guid>
		<description>Brian, I've had the same experience with MySQL as well. Syntax sugar and optimizer don't seem to play all that well at the moment.

Matt, I just tested both queries independently (without ActiveRecord) on cold-start mysqld instances: same results as above. I wonder if I stumbled across a platform/version dependent bug. I'll try to dig deeper into it over the weekend and see what I can uncover.

The joys of SQL. :)</description>
		<content:encoded><![CDATA[<p>Brian, I&#8217;ve had the same experience with MySQL as well. Syntax sugar and optimizer don&#8217;t seem to play all that well at the moment.</p>
<p>Matt, I just tested both queries independently (without ActiveRecord) on cold-start mysqld instances: same results as above. I wonder if I stumbled across a platform/version dependent bug. I&#8217;ll try to dig deeper into it over the weekend and see what I can uncover.</p>
<p>The joys of SQL. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://www.igvita.com/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-67314</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Fri, 07 Sep 2007 19:06:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.igvita.com/blog/2007/09/06/subqueries-and-sql-syntax-sugar-diet/#comment-67314</guid>
		<description>Re: Chris's comment above. Yeah, that was my thought when I first saw this too. Ilya, any chance you could try switching the order of the finds around and re-testing to put our minds at ease?

My hunch is that there's something else going on here, and the fact that you're arriving at your results through ActiveRecord is only serving to cloud the matter. I tried a quick test directly in the MySQL client with a set of 1000 ids, but my queries were completing in under a second so the differences weren't significant enough to call.</description>
		<content:encoded><![CDATA[<p>Re: Chris&#8217;s comment above. Yeah, that was my thought when I first saw this too. Ilya, any chance you could try switching the order of the finds around and re-testing to put our minds at ease?</p>
<p>My hunch is that there&#8217;s something else going on here, and the fact that you&#8217;re arriving at your results through ActiveRecord is only serving to cloud the matter. I tried a quick test directly in the MySQL client with a set of 1000 ids, but my queries were completing in under a second so the differences weren&#8217;t significant enough to call.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
