<?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>Snow Giraffe Tech &#187; GROUP BY</title>
	<atom:link href="http://www.snowgiraffe.com/tech/tag/group-by/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.snowgiraffe.com/tech</link>
	<description>rails, rubies, and sometimes dolphins</description>
	<lastBuildDate>Mon, 07 Jun 2010 14:36:05 +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>Aliasing MySQL Functions with ActiveRecord</title>
		<link>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/</link>
		<comments>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 18:20:43 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[:group]]></category>
		<category><![CDATA[:include]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[aliasing]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[eager loading]]></category>
		<category><![CDATA[GROUP BY]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=117</guid>
		<description><![CDATA[Changing column data to use MySQL functions instead of the actual column data for Ruby on Rails ActiveRecord is simple. To allow ActiveRecord to retrieve the data of the function,  alias it to the column name.
Event.find :all, :select =&#62; 'substring(name, 1, 10) as name'
This produces the follow SQL query using MySQL SUBSTRING function:
select substring(name, 1, [...]]]></description>
			<content:encoded><![CDATA[<p>Changing column data to use MySQL functions instead of the actual column data for Ruby on Rails ActiveRecord is simple. To allow ActiveRecord to retrieve the data of the function,  alias it to the column name.</p>
<pre><span class="constant">Event</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">substring(name, 1, 10) as name</span><span class="punct">'</span></pre>
<p>This produces the follow SQL query using MySQL <span class="inline_code">SUBSTRING</span> function:</p>
<pre><span class="mysql">select substring(name, 1, 10) as name from events; </span></pre>
<p>ActiveRecord will pull the truncated <span class="inline_code">name</span> (the first 10 characters) in the function data into the ActiveRecord field <span class="inline_code">name</span>. This can be very useful for offloading some work to <a href="http://dev.mysql.com/doc/refman/5.0/en/functions.html" target="_blank">MySQL by using its operations</a> rather than performing the same functions in memory with Rails code.</p>
<p>Aliasing methods can be useful when combined with the <span class="inline_code">GROUP BY </span> functionality. For example, the following query will count the number of events that start on the same day.</p>
<pre><span class="ident">events</span> <span class="punct">=</span> <span class="constant">Event</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">DATE(start_time) as start_time, count(*) as num_events</span><span class="punct">',</span> <span class="symbol">:group</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">DATE(start_time)</span><span class="punct">'</span></pre>
<h2>Notes and Caveats</h2>
<p>Be aware that several issues exist when using MySQL aliasing with ActiveRecord.</p>
<p><span id="more-117"></span></p>
<h3>Eager Loading</h3>
<p>One note is is that ActiveRecord doesn&#8217;t support <span class="inline_code">:select</span> with eager loading. This means that the <span class="inline_code">:select</span> clause is ignored when <span class="inline_code">:include</span> is specified. To work around this problem (and get a performance benefit by selecting fewer columns), install one of the many eager loading plugins referenced on this <a href="http://dev.rubyonrails.org/ticket/5371 ">ticket</a>. Perhaps there are better ones, but I wrote <a href="http://arperftoolkit.rubyforge.org/svn/trunk/eload_select">eload select</a> and have stuck to it.</p>
<pre>script/plugin install http://arperftoolkit.rubyforge.org/svn/trunk/eload_select</pre>
<h3>Aliased Column vs. MySQL Function</h3>
<p>Another caveat is that due to a <a href="http://bugs.mysql.com/bug.php?id=188">MySQL &#8220;Not a Bug&#8221;</a>, the full function <span class="inline_code">DATE(start_time)</span> must be specified instead of just the alias <span class="inline_code">start_time</span>.  This might seem obvious, but the  <span class="inline_code&gt;GROUP BY&lt;/span&gt; clause:&lt;/p&gt; &lt;pre&gt;&lt;span class=&quot;symbol&quot;&gt;:group&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;DATE(start_time)&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&quot;&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;Using the aliased name start_time use the actual column name instead of the aliased function. This would be more intuitive if &lt;span class=">ORDER BY</span> worked the same way. However,  <span class="inline_code">ORDER BY</span> clause does the opposite and uses the <em> function</em> aliased to <span class="inline_code">name</span> instead of the column <span class="inline_code">name</span>. For example,here the data is sorted on the second letter of the title (the substring function).</p>
<pre><span class="constant">Event</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">substring(title, 2) as title</span><span class="punct">',</span> <span class="symbol"> <img src='http://www.snowgiraffe.com/tech/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder</span> <span class="punct">=&gt;'</span><span class="string">title</span><span class="punct">'</span></pre>
<p>which produces this SQL query:</p>
<pre><span class="mysql">select substring(title, 2) as title from events order by title;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
