<?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>Erics Tech Blog &#187; database</title>
	<atom:link href="http://eric.lubow.org/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.lubow.org</link>
	<description>Thoughts, musings, and other idealistic (sometimes useful) systems and development hoopla.</description>
	<lastBuildDate>Fri, 18 Nov 2011 14:56:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>ec2-consistent-snapshot With Mongo</title>
		<link>http://eric.lubow.org/2011/databases/mongodb/ec2-consistent-snapshot-with-mongo/</link>
		<comments>http://eric.lubow.org/2011/databases/mongodb/ec2-consistent-snapshot-with-mongo/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 07:00:47 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=863</guid>
		<description><![CDATA[I setup MongoDB on my Amazon EC2 instance knowing full well that it would have to be backed up at some point. I also knew that by using XFS, I could take advantage of filesystem freezing in a similar fashion to LVM snapshots. I had remembered reading about backups on XFS with MySQL being done [...]]]></description>
			<content:encoded><![CDATA[<p>I setup <a href="http://www.mongodb.org/">MongoDB</a> on my Amazon EC2 instance knowing full well that it would have to be backed up at some point.  I also knew that by using XFS, I could take advantage of filesystem freezing in a similar fashion to LVM snapshots.  I had remembered reading about backups on XFS with MySQL being done with <a href="http://alestic.com/2009/09/ec2-consistent-snapshot">ec2-consistent-snapshot</a>.  As with any piece of open source software, it just took a little tweaking to make it do what I wanted it to do.<br />
<span id="more-863"></span><br />
Out of the box, ec2-consistent-snapshot works great for freezing an XFS filesystem with MySQL because it not only stops the server, but handles potential replication issues.  By following the steps outlined <a href="http://www.mongodb.org/pages/viewpage.action?pageId=19562846">here</a> by 10gen, I just made  a few slight adjustments to the core ec2-consistent snapshot script to allow for MongoDB support.  In fact, it supports locking and fsyncing immediately prior to freezing and backup.  I have been using this script in production for a while now and it seems to work without issue for me.</p>
<p>In the usual spirit of social coding, I have added the script to Github: <a href="https://github.com/elubow/ec2-consistent-snapshot">https://github.com/elubow/ec2-consistent-snapshot</a>.</p>
<p>Running it is just this:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ec2-consistent-snapshot &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\<br />
<span style="color: #660033;">--mongo</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\<br />
<span style="color: #660033;">--xfs-filesystem</span> <span style="color: #000000; font-weight: bold;">/</span>data &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \<br />
<span style="color: #660033;">--region</span> us-east-<span style="color: #000000;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \<br />
<span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;RAID snapshot <span style="color: #007800;">$(date +'%Y-%m-%d %H:%M:%S')</span>&quot;</span> \<br />
vol-VOL1 vol-VOL2 vol-VOL3 vol-VOL4 vol-VOL5 vol-VOL6 vol-VOL7 vol-VOL8</div></div>
<p>The options used here (for reference) are telling ec2-consistent-snapshot to use <em>&#8211;mongo</em>, on the <em>&#8211;xfs-filesystem</em> /data, in the us-east-1 <em>&#8211;region</em> (note that it&#8217;s just the region and not the availability zone within that region), to be backed up with the listed <em>&#8211;description</em> of the specified volumes.  You can even throw a <em>&#8211;mongo-stop</em> in there to have Mongo stopped before the file system freeze and then restarted after the volumes have been backed up.  Don&#8217;t forget that you need to set your Amazon keys in you environment variables (AMAZON_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY for your key and secret respectively).</p>
<p>I attempted to keep the usage style consistent with Eric Hammond&#8217;s original version, just add Mongo support for it.</p>
<p><strong>Note:</strong> I also mentioned this on the <a href="http://groups.google.com/group/mongodb-user/browse_thread/thread/633c3fbc648861a1?pli=1">mailing list</a>.  But given the amount of messages that fly around on the list daily, some folks may have missed it.</p>
<p><strong>References:</strong></p>
<ul>
<li><a href="http://alestic.com/2009/09/ec2-consistent-snapshot">ec2-consistent-snapshot</a> blog entry by Eric Hammond</li>
<li><a href="https://github.com/elubow/ec2-consistent-snapshot">ec2-consistent-snapshot</a> on Github with Mongo DB support</li>
<li><a href="http://www.mongodb.org/pages/viewpage.action?pageId=19562846">Backing up MongoDB on EC2 (10gen)</a></li>
</ul>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/databases/mongodb/getting-a-random-record-from-a-mongodb-collection/' rel='bookmark' title='Getting a Random Record From a MongoDB Collection'>Getting a Random Record From a MongoDB Collection</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2011/databases/mongodb/ec2-consistent-snapshot-with-mongo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Massachusetts Security Law Passed For Databases</title>
		<link>http://eric.lubow.org/2010/databases/new-massachusetts-security-law-passed-for-databases/</link>
		<comments>http://eric.lubow.org/2010/databases/new-massachusetts-security-law-passed-for-databases/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 10:00:28 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[legal]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=701</guid>
		<description><![CDATA[In case you haven&#8217;t heard about the new Massachusetts state law regarding consumer or client information in databases, you can read about it here, at Information Week, or just Google for &#8220;Massachusetts data security law&#8221;. And if you haven&#8217;t read about, then I strongly suggest you do. This is one of those instances where I [...]]]></description>
			<content:encoded><![CDATA[<p>In case you haven&#8217;t heard about the new Massachusetts state law regarding consumer or client information in databases, you can read about it <a href="http://www.sqlmag.com/print/sql-server/A-New-Law-that-Will-Change-the-Way-You-Build-Database-Applications.aspx">here</a>, at <a href="http://www.informationweek.com/news/security/government/showArticle.jhtml?articleID=224400426&#038;queryText=massachusetts%20cmr">Information Week</a>, or just <a href="http://google.com/">Google</a> for &#8220;Massachusetts data security law&#8221;.  And if you haven&#8217;t read about, then I strongly suggest you do.  This is one of those instances where I believe their heart is in the right place, even if the execution/implementation wasn&#8217;t perfect.<br />
<span id="more-701"></span><br />
I get the feeling that Mass will make an example of a few offenders and then hope that the law either gets picked up by other states or federalized.  The problem is that this law will only really affect companies that are headquartered in Mass.  I am by no means a lawyer, but I don&#8217;t believe that its legal for the state of Mass to go after companies that are headquartered in other states.</p>
<p>Now at the very least, this should be a reminder to developers and sysadmins to make sure that data is properly protected, both in storage and transfer, and that is properly managed within an application.  Although this is a scary prospect that the government is getting involved in software design and data management, it certainly isn&#8217;t the first time (think HIPAA, FIPS testing, and Sarbanes–Oxley).  Although those have been helpful, the goals seemed a little clearer.</p>
<p>This law is going to put a pretty heavy imposition on smaller organizations with regard to user education and basic requirements fulfillment for data storage.  It&#8217;s going to be quite a bit harder to bring a product to market in Mass.  Although it seems like the government is doing a service, here they may be doing a disservice to their state economy.</p>
<p>I guess we&#8217;ll have to see how this plays out.  Thoughts? </p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/country-state-select-using-carmen-and-jquery/' rel='bookmark' title='Country-State Select Using Carmen and jQuery'>Country-State Select Using Carmen and jQuery</a></li>
<li><a href='http://eric.lubow.org/2010/book-reviews/mod-security-2-5-by-magnus-mischel/' rel='bookmark' title='Mod-Security 2.5 by Magnus Mischel'>Mod-Security 2.5 by Magnus Mischel</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/databases/new-massachusetts-security-law-passed-for-databases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speeding Up Your Selects and Sorts</title>
		<link>http://eric.lubow.org/2010/databases/mysql/speeding-up-your-selects-and-sorts/</link>
		<comments>http://eric.lubow.org/2010/databases/mysql/speeding-up-your-selects-and-sorts/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 13:30:07 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql tricks]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=585</guid>
		<description><![CDATA[When you are using a framework, they typically set your VARCHAR size automatically to 255. This is normally fine since you are letting the framework abstract you away from most of the SQL. But if you interact with your SQL, there is a way to get a decent speed increase on your SELECTs and ORDER [...]]]></description>
			<content:encoded><![CDATA[<p>When you are using a framework, they typically set your VARCHAR size automatically to <strong>255</strong>.  This is normally fine since you are letting the framework abstract you away from most of the SQL.  But if you interact with your SQL, there is a way to get a decent speed increase on your SELECTs and ORDER BYs when you are working with VARCHARs.</p>
<p>The VARCHAR data type is only variable character size for storage, not for sorting and buffering.  In fact, since the MySQL optimizer doesn&#8217;t know how big the data in that column can be, it has to allocate the maximum size possible for that column.  So for sorting and buffering of the <em>name</em> and <em>email</em> columns below would take up <strong>310</strong> bytes per row.<br />
<span id="more-585"></span></p>
<p>To fix that you should alter the size of your columns.  Imagine having the optimizer have to go through an additional 310 bytes for every row.  If there are 500k rows in the table time 310 bytes for each row, that can add up in the amount of memory that the optimizer has to use to perform the sorting/buffering.</p>
<p>Consider the following table of businesses:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">DESC</span> businesses;<br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------+--------------+------+-----+---------+----------------+</span><br />
<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">FIELD</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TYPE</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #66cc66;">|</span> Extra &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span><br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------+--------------+------+-----+---------+----------------+</span><br />
<span style="color: #66cc66;">|</span> id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> NO &nbsp; <span style="color: #66cc66;">|</span> PRI <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> url &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> email &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> description &nbsp; &nbsp;<span style="color: #66cc66;">|</span> text &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> created_at &nbsp; &nbsp; <span style="color: #66cc66;">|</span> datetime &nbsp; &nbsp; <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> updated_at &nbsp; &nbsp; <span style="color: #66cc66;">|</span> datetime &nbsp; &nbsp; <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------+--------------+------+-----+---------+----------------+</span><br />
<span style="color: #cc66cc;">7</span> <span style="color: #993333; font-weight: bold;">ROWS</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span><br />
<br />
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>email<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> businesses;<br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------+--------------------+------------------+</span><br />
<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>email<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span><br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------+--------------------+------------------+</span><br />
<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">53</span> <span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">36</span> <span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------+--------------------+------------------+</span><br />
<span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">ROW</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.40</span> sec<span style="color: #66cc66;">&#41;</span></div></div>
<p>You can change the name to column sizes to 75 (name), 50 (email), and 100 (url).  But realistically you don&#8217;t want to chance things getting cut off, so it may be better off to settle on each column here being a VARCHAR(100).  Even that would save drastically on the space required to perform a sort and buffer the results.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2008/databases/mysql/mysql-encoded-uri-search-and-replace/' rel='bookmark' title='MySQL Encoded URI Search and Replace'>MySQL Encoded URI Search and Replace</a></li>
<li><a href='http://eric.lubow.org/2010/perl/perl-modules/using-unique-keys-and-key-groups-with-background-jobs-in-gearmanclient/' rel='bookmark' title='Using Unique Keys and Key Groups with Background Jobs in Gearman::Client'>Using Unique Keys and Key Groups with Background Jobs in Gearman::Client</a></li>
<li><a href='http://eric.lubow.org/2009/system-administration/howto-recreate-devnull/' rel='bookmark' title='HOWTO Recreate /dev/null'>HOWTO Recreate /dev/null</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/databases/mysql/speeding-up-your-selects-and-sorts/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Model Specific Formatted Search Results Using Thinking Sphinx</title>
		<link>http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/</link>
		<comments>http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:15:23 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=538</guid>
		<description><![CDATA[Having recently implemented Thinking Sphinx on one of my web sites, I thought it would be cool to be able to search every indexed model. With Thinking Sphinx, it&#8217;s easy to have a bunch of different classes returned in the results. The tougher part is displaying them in a way that is organized (although admittedly [...]]]></description>
			<content:encoded><![CDATA[<p>Having recently implemented <a href="http://freelancing-god.github.com/ts/en">Thinking Sphinx</a> on one of my web sites, I thought it would be cool to be able to search every indexed model.  With Thinking Sphinx, it&#8217;s easy to have a bunch of different classes returned in the results.  The tougher part is displaying them in a way that is organized (although admittedly not very DRY).<br />
<span id="more-538"></span><br />
Let&#8217;s start out with the controller method for search results (<strong>app/controllers/search_controller.rb</strong>):</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> result<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> !params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:model</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">blank</span>?<br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@model</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:model</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@model</span> = <span style="color:#996600;">&quot;ThinkingSphinx&quot;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@query</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:search</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:query</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@results</span> = Search.<span style="color:#9900CC;">model_search</span><span style="color:#006600; font-weight:bold;">&#40;</span>@model, <span style="color:#0066ff; font-weight:bold;">@query</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Also, let&#8217;s make sure we define the actual search model.  I define it in a separate search library (<strong>lib/search.rb</strong>).  This is the relevant snippet of code:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">model_search</span><span style="color:#006600; font-weight:bold;">&#40;</span>model, keywords, var = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@search_options</span> = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> var<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:page</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:per_page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">15</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@search_options</span>.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;@relevance DESC&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:sort_mode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:expr</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:sort_by</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;@weight * @weight&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; model.<span style="color:#9900CC;">constantize</span>.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span>keywords, <span style="color:#0066ff; font-weight:bold;">@search_options</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Assuming that we are working with the all models search, where the above model is <em>ThinkingSphinx</em>, let&#8217;s iterate over the search results with this code in the view (<strong>app/views/search/results.html.erb</strong>):</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@results</span>.<span style="color:#9900CC;">total_entries</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span>div id=<span style="color:#996600;">&quot;localLocationList&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>ul <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;list&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#0066ff; font-weight:bold;">@results</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>li<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= display_search_result<span style="color:#006600; font-weight:bold;">&#40;</span>result<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;/</span>li<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;/</span>ul<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>div <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;clear&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;/</span>div<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;/</span>div<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>
<p>And the view helper (<strong>app/helpers/search_helper.rb</strong>):</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">def</span> display_search_result<span style="color:#006600; font-weight:bold;">&#40;</span>result<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;render :partial =&gt; '#{result.class.to_s.downcase.pluralize}/result',<br />
&nbsp; &nbsp; :locals =&gt; { :result =&gt; result }&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>The interesting thing about this bit of code is the <strong>eval</strong>.  The <strong>eval</strong> on each iteration decides which search result partial to display based on the class and then passes the result to the partial for display.  So if the result has a class of <em>Business</em>, the partial <strong>app/views/businesses/_result.html.erb</strong> will be rendered.  This is a quick example of a search result partial:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;</span>span id=<span style="color:#996600;">&quot;localBizName&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= <span style="color:#5A0A0A; font-weight:bold;">link_to</span> result.<span style="color:#9900CC;">name</span>, business_path<span style="color:#006600; font-weight:bold;">&#40;</span> result <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;&lt;/</span>span<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;smallOffset&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= truncate<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;#{result.description}&quot;</span>, :<span style="color:#5A0A0A; font-weight:bold;">length</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">128</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span></div></div>
<p>This is useful because all models don&#8217;t have the same characteristics.  By creating a search result partial for each model type, this can be reused for consistent looking search results around your webapp.  If the search is model specific, the same result partial will be used in every iteration over the results.  If the search is model agnostic, then you can display your search results in a consistent manner.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/seo/sortfix-or-a-next-generation-search-tool/' rel='bookmark' title='Sortfix Or A Next Generation Search Tool'>Sortfix Or A Next Generation Search Tool</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/adding-an-average-column-to-a-model/' rel='bookmark' title='Adding An Average Column To A Model'>Adding An Average Column To A Model</a></li>
<li><a href='http://eric.lubow.org/2008/databases/mysql/mysql-encoded-uri-search-and-replace/' rel='bookmark' title='MySQL Encoded URI Search and Replace'>MySQL Encoded URI Search and Replace</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When To Use MySQL Cursor Classes In Python</title>
		<link>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/</link>
		<comments>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 15:00:40 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=526</guid>
		<description><![CDATA[I have been writing a lot of code that has been interacting with MySQL lately. Sometimes I find it easier to work the result set in a dictionary form and other times it is easier with an array. But in order to not break all your code, it is necessary to set a default cursor [...]]]></description>
			<content:encoded><![CDATA[<p>I have been writing a lot of code that has been interacting with MySQL lately.  Sometimes I find it easier to work the result set in a dictionary form and other times it is easier with an array.  But in order to not break all your code, it is necessary to set a default cursor class that keeps your code consistent.  More often than not, I find using using a arrays is easier since I just want quick access to all the retrieved data.  I also end up making my SELECT calls while specifying the columns and order of the columns I want returned.</p>
<p>The reason that using cursor classes is handy is because Python doesn&#8217;t come with a <strong>mysql_fetch_assoc</strong> like PHP or <strong>selectrow_hashref</strong> like Perl&#8217;s DBI interface.  Python uses cursor dictionaries to bridge this gap.  Ultimately your result is the same.  But as with Perl and PHP, defaulting to cursor dictionaries isn&#8217;t a good idea for larger datasets because of the extra processing time and memory required to convert the data.<br />
<span id="more-526"></span></p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<br />
conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost'</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; passwd<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'secret'</span><span style="color: #66cc66;">,</span> db<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'test'</span><span style="color: black;">&#41;</span><br />
cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT this,that FROM foobar&quot;</span><span style="color: black;">&#41;</span><br />
rows <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>This then allows me to do just call the DictCursor (dictionary based cursor) on a single cursor object for a particular query and leave all the rest of the queries as array based results.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb.<span style="color: black;">cursors</span><br />
<br />
conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost'</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; passwd<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'secret'</span><span style="color: #66cc66;">,</span> db<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'test'</span><span style="color: black;">&#41;</span><br />
cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span>cursorclass<span style="color: #66cc66;">=</span>MySQLdb.<span style="color: black;">cursors</span>.<span style="color: black;">DictCursor</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT this,that FROM foobar&quot;</span><span style="color: black;">&#41;</span><br />
rows <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> rows:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> row<span style="color: black;">&#91;</span><span style="color: #483d8b;">'this'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> row<span style="color: black;">&#91;</span><span style="color: #483d8b;">'that'</span><span style="color: black;">&#93;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p><strong>Note:</strong> Doing something like changing the connection string to use a default cursor class of dictionary cursor in an existing code base is not a good idea.  It will make your results come back as dictionaries when your current code expects arrays.  Yes, I did this and had to track it down the hard way. Hopefully this saves you some trouble.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost'</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; passwd<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'secret'</span><span style="color: #66cc66;">,</span> db<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'test'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; cursorclass<span style="color: #66cc66;">=</span>MySQLdb.<span style="color: black;">cursors</span>.<span style="color: black;">DictCursor</span><span style="color: black;">&#41;</span></div></div>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/' rel='bookmark' title='Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync'>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/' rel='bookmark' title='Database Read/Write Splitting in Frameworks/ORMs'>Database Read/Write Splitting in Frameworks/ORMs</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Migrations Without belongs_to Or references</title>
		<link>http://eric.lubow.org/2009/ruby/rails/migrations-without-belongs_to-or-references/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/migrations-without-belongs_to-or-references/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 14:00:00 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=308</guid>
		<description><![CDATA[Normally when do a database migration in Rails, when adding ownership from a model to another model, you use the concept of belongs_to or references: &#160; create_table :comments do &#124;t&#124; &#160; &#160; t.belongs_to :user &#160; &#160; t.references :post &#160; end Interestingly enough, these methods are only available during the initial table creation. If you want [...]]]></description>
			<content:encoded><![CDATA[<p>Normally when do a database migration in Rails, when adding ownership from a model to another model, you use the concept of belongs_to or references:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:comments</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; t.<span style="color:#9900CC;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:user</span><br />
&nbsp; &nbsp; t.<span style="color:#9900CC;">references</span> <span style="color:#ff3333; font-weight:bold;">:post</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Interestingly enough, these methods are only available during the initial table creation.  If you want to add a reference to a model that is created later, you have to do it the old fashioned way, by just adding a column:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;add_column <span style="color:#ff3333; font-weight:bold;">:comments</span>, <span style="color:#ff3333; font-weight:bold;">:group_id</span>, :<span style="color:#CC0066; font-weight:bold;">integer</span></div></div>
<p>Doing it this way is clean, easy, and definitely meets the KISS principle.  But I do find it interesting that one can&#8217;t add an association later in the game.  Sometimes the Rails way is just KISS and adding the column by hand.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/' rel='bookmark' title='Model Specific Formatted Search Results Using Thinking Sphinx'>Model Specific Formatted Search Results Using Thinking Sphinx</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/' rel='bookmark' title='Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)'>Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/migrations-without-belongs_to-or-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

