<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>http://regedor.com/ &#187; Improve Performance</title>
	<atom:link href="http://regedor.com/tag/improve-performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://regedor.com</link>
	<description>Just another weblog written by an human</description>
	<lastBuildDate>Mon, 02 Nov 2009 19:42:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='regedor.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/7fc9f61f922100cfbf3dcbd3d66a0986?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>http://regedor.com/ &#187; Improve Performance</title>
		<link>http://regedor.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://regedor.com/osd.xml" title="http://regedor.com/" />
	<atom:link rel='hub' href='http://regedor.com/?pushpress=hub'/>
		<item>
		<title>Get all values of a single column efficiently</title>
		<link>http://regedor.com/2009/02/25/get-all-values-of-a-single-column-efficiently/</link>
		<comments>http://regedor.com/2009/02/25/get-all-values-of-a-single-column-efficiently/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 23:12:03 +0000</pubDate>
		<dc:creator>Regedor</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Improve Performance]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://miguelregedor.wordpress.com/?p=76</guid>
		<description><![CDATA[Imagine you want this: @names = MyModel.find(:all).map{ &#124;i&#124; i.name }.uniq Yes, that works! You get all distinct names of one column, you can even do something like that: #Using Shortcut Blocks with Symbol to_proc @names = MyModel.all.map(&#38;:name).uniq Cool, rely small code(almost seems I&#8217;m using Haskel) but not that efficient if your model&#8217;s table has lots [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=regedor.com&amp;blog=2140217&amp;post=76&amp;subd=miguelregedor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Imagine you want this:</p>
<p><code></p>
<pre style='color:#000000;background:#ffffff;'>@names <span style='color:#808030;'>=</span> MyModel<span style='color:#808030;'>.</span>find<span style='color:#808030;'>(</span>:all<span style='color:#808030;'>)</span><span style='color:#808030;'>.</span>map<span style='color:#800080;'>{</span> |i| i<span style='color:#808030;'>.</span>name <span style='color:#800080;'>}</span><span style='color:#808030;'>.</span>uniq
</pre>
<p></code></p>
<p>Yes, that works! You get all distinct names of one column, you can even do something like that:</p>
<p><code></p>
<pre style='color:#000000;background:#ffffff;'><span style='color:#696969;'>#Using Shortcut Blocks with Symbol to_proc</span>
@names <span style='color:#808030;'>=</span> MyModel<span style='color:#808030;'>.</span>all<span style='color:#808030;'>.</span>map<span style='color:#808030;'>(</span>&amp;:name<span style='color:#808030;'>)</span><span style='color:#808030;'>.</span>uniq
</pre>
<p></code></p>
<p>Cool, rely small code(almost seems I&#8217;m using Haskel) but not that efficient if your model&#8217;s table has lots of columns. (Imagine! The whole table is being loaded to memory) So what is the solution?</p>
<p><code></p>
<pre style='color:#000000;background:#ffffff;'>MyModel<span style='color:#808030;'>.</span>find<span style='color:#808030;'>(</span> :all, :<span style='color:#400000;'>select</span> &gt; <span style='color:#0000e6;'>'DISTINCT name'</span> <span style='color:#808030;'>)</span>
</pre>
<p></code></p>
<p>If you specify wich columns you need, in the sql query, you will spare some memory. And in my earlier example you can also pass the &#8220;uniq&#8221; work to the database.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/miguelregedor.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/miguelregedor.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/miguelregedor.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/miguelregedor.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/miguelregedor.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/miguelregedor.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/miguelregedor.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/miguelregedor.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/miguelregedor.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/miguelregedor.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/miguelregedor.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/miguelregedor.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/miguelregedor.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/miguelregedor.wordpress.com/76/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=regedor.com&amp;blog=2140217&amp;post=76&amp;subd=miguelregedor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://regedor.com/2009/02/25/get-all-values-of-a-single-column-efficiently/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/80583db5af674ca20393f42a20101129?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Regedor</media:title>
		</media:content>
	</item>
	</channel>
</rss>