<?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; find</title>
	<atom:link href="http://www.snowgiraffe.com/tech/tag/find/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.snowgiraffe.com/tech</link>
	<description>rails, rubies, and sometimes dolphins</description>
	<lastBuildDate>Mon, 17 Oct 2011 17:45:19 +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>ActiveRecord on Steroids: Optimize queries using Ar-Extensions 0.9.1</title>
		<link>http://www.snowgiraffe.com/tech/391/activerecord-on-speed-ar-extensions/</link>
		<comments>http://www.snowgiraffe.com/tech/391/activerecord-on-speed-ar-extensions/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 21:42:42 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ar-extensions]]></category>
		<category><![CDATA[:select]]></category>
		<category><![CDATA[database optimization]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[INSERT]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[UPDATE]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=391</guid>
		<description><![CDATA[The ar-extensions plugin extends  ActiveRecord to help developers scale, optimize, and customize Rails interaction with the database. New ar-extensions is the ability to fine tune queries by specifying MySQL database options from ActiveRecord find and save methods.]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-medium wp-image-438" title="legos" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/legos-300x216.jpg" alt="legos" width="240" height="173" />The newly released <a href="http://github.com/zdennis/ar-extensions">ar-extensions version 0.9.1</a> includes a handful of new goodies I ported and tweaked and merged from now deprecated <a href="http://arperftoolkit.rubyforge.org/svn/">arperftoolkit</a>, my original toolbox of ActiveRecord tricks we&#8217;ve been using at <a href="http://spongecell.com">Spongecell</a> for over two years. The database agnostic <a href="http://github.com/zdennis/ar-extensions" target="_blank"> ar-extensions plugin</a> and <a href="http://rubyforge.org/frs/?group_id=2113">gem</a> by <a href="http://continuousthinking.com" target="_blank">Zach Dennis</a> extends and enhances the functionality of ActiveRecord to provide developers with a bag of tricks to help scale, optimize, and customize Rails interaction with the database.</p>
<pre>script/plugin install git://github.com/zdennis/ar-extensions.git
gem install ar-extensions</pre>
<p>While the original functionality of database agnostic <a href="http://github.com/zdennis/ar-extensions/tree/master">ar-extensions</a> is to import (bulk insert) many records quickly, my favorite new features for 0.9.1 are the <span style="color: #ff6600;">new find and save options</span> which allow developers to quickly customize queries without writing SQL for the MySQL database.</p>
<h2>Find</h2>
<p>For instance, suppose the wrong index is used to perform your favorite query. This use to mean that the developer had to dig through the logs, find the generated query, and rewrite the entire query with the correct index in SQL and execute it with <span class="inline_code">ActiveRecord::Base.find_by_sql</span>. With ar-extensions, its just another parameter to the find method. Similarly, leveraging MySQL functionality such as <span class="inline_code">SQL_CACHE</span>, <span class="inline_code">HIGH PRIORITY</span>, and <span class="inline_code">LOCK IN SHARED MODE</span> is just as easy.</p>
<pre class="ruby"><span class="constant">Beer</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:index_hint</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">USE INDEX (uk_beer_name)</span><span class="punct">'
#SQL: SELECT * from `beers` USE INDEX (uk_beer_name)
</span>
<span class="constant">Beer</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:keywords</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string"> SQL_CACHE HIGH_PRIORITY</span><span class="punct">'
#SQL: </span><span class="punct">SELECT SQL_CACHE HIGH_PRIORITY * from `beers` </span><span class="punct"> </span>

<span class="constant">Beer</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:post_sql</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string"> FOR UPDATE</span><span class="punct">',</span> <span class="symbol">:pre_sql</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">/* My little comment*/</span><span class="punct">'</span>
<span class="comment">#SQL: /* My little comment*/ SELECT * from `beers` FOR UPDATE </span></pre>
<h2>Finder SQL Exposed</h2>
<p>And if that&#8217;s not enough to take advantage of<a href="http://dev.mysql.com/doc/refman/5.1/en/select.html" target="_blank"> MySQL query options</a>, <span class="inline_code">finder_sql_to_string</span> will send the query string back and you can gsub your heart out before execution. It&#8217;s also a great to use for debugging purposes.</p>
<pre class="ruby"><span class="ident">sql</span> <span class="punct">=</span> <span class="constant">Beer</span><span class="punct">.</span><span class="ident">send</span> <span class="symbol">:finder_sql_to_string</span><span class="punct">,</span> <span class="symbol">:conditions</span> <span class="punct">=&gt;</span> <span class="punct">['</span><span class="string">flavor like ?</span><span class="punct">',</span> <span class="punct">'</span><span class="string">delicious</span><span class="punct">']</span>
<span class="constant">Beer</span><span class="punct">.</span><span class="ident">find_by_sql</span> <span class="ident">sql</span><span class="punct">.</span><span class="ident">gsub</span><span class="punct">('</span><span class="string">WHERE</span><span class="punct">',</span> <span class="punct">'</span><span class="string">where /* I heart lowercase */</span><span class="punct">')</span></pre>
<h2>Save and Create</h2>
<p>As for save and create, the new options provide more control over database <a href="http://dev.mysql.com/doc/refman/5.1/en/insert.html" target="_blank">inserts</a> and <a href="http://dev.mysql.com/doc/refman/5.1/en/update.html">updates</a>, especially when unique keys are involved.</p>
<pre class="ruby"><span class="constant">Animal</span><span class="punct">.</span><span class="ident">create!</span><span class="punct">({</span><span class="symbol">:name</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">Jerry Giraffe</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' /> n_duplicate_key_update</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:password</span><span class="punct">])</span>
<span class="comment">#INSERT INTO animals (`name`, `fav_beer`, `password`) VALUES('Jerry Giraffe', 'Pabst', NULL) ON DUPLICATE KEY UPDATE `animals`.`password`=VALUES(`password`)</span>

<span class="ident">animal</span><span class="punct">.</span><span class="ident">save</span><span class="punct">(</span><span class="symbol">:keywords</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">LOW_PRIORITY</span><span class="punct">',</span> <span class="symbol">:ignore</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>
            <span class="symbol">:pre_sql</span> <span class="punct">=&gt;</span> <span class="punct">"</span><span class="string">/* Now I know where this query is coming from within my Rails code!*/</span><span class="punct">")</span>
<span class="comment">#/* Now I know where this query is coming from within my Rails code!*/ UPDATE LOW_PRIORITY IGNORE `animals` SET `fav_beer` = 'Pabst', `password` = 'frenchfry', `name` = 'Party Giraffe' WHERE `id` = 1</span></pre>
<p>In addition to these spiffy new features, <a href="http://github.com/zdennis/ar-extensions/tree/master">ar-extensions</a> provides support for all of these:</p>
<ul>
<li> <span style="color: #ff0000;">(new)</span> <a href="http://www.snowgiraffe.com/tech/391/activerecord-o…ensions-pluginactiverecord-on-speed-new-tools-for-the-ar-extensions-plugin/" target="_blank">create and save options</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/create_and_update_rb.html" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new)</span> <a href="http://www.snowgiraffe.com/tech/391/activerecord-o…ensions-pluginactiverecord-on-speed-new-tools-for-the-ar-extensions-plugin/" target="_blank">find options</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/finder_options_rb.html" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new)</span> <a href="http://www.snowgiraffe.com/tech/418/ar-extensions-activerecordbaseinsert_select/">insert_select</a> from one table to another <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000048" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new) </span>find and count unions <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000053" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new)</span> delete options <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000038" target="_blank">rdoc</a></li>
<li> <a title="Import" href="http://www.continuousthinking.com/2007/5/6/activerecord-extensions-0-6-0-released" target="_blank">import (bulk insert) with synchronization</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000044" target="_blank">rdoc</a></li>
<li> <a href="http://www.continuousthinking.com/2007/1/31/activerecord-temporary-tables-and-merging-mysql" target="_blank">csv export</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Extensions/FindToCSV.html" target="_blank">rdoc</a></li>
<li> <a title="Better Finder Hash Support" href="http://www.continuousthinking.com/2007/3/14/activerecord-extensions-0-5-0-released" target="_blank">better finder hash support</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Extensions.html" target="_blank">rdoc</a></li>
<li> <a title="Disabling Foreign Keys" href="http://www.continuousthinking.com/2007/1/31/activerecord-temporary-tables-and-merging-mysql" target="_blank">disabling foreign keys</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/foreign_keys_rb.html" target="_blank">rdoc</a></li>
<li>fulltext search support <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Extensions/FullTextSearching.html" target="_blank">rdoc</a></li>
<li><a title="Temporary Table Nicety" href="http://www.continuousthinking.com/2007/1/31/activerecord-temporary-tables-and-merging-mysql" target="_blank">temporary table manipulation</a> <a title="Temporary Table rdoc" href="file:///Users/blythie/radrails/ar_test/ar_test/vendor/plugins/ar-extensions/ar-extensions/doc/classes/ActiveRecord/Base.html#M000046" target="_blank"> rdoc</a></li>
</ul>
<p>That&#8217;s a lot of rope! If you do by chance hang yourself, please send bugs, comments and even patches to the <a title="Github issue tracker for Ar-Extensions" href="http://github.com/zdennis/ar-extensions/issues" target="_blank">new github issue tracker</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/391/activerecord-on-speed-ar-extensions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

