<?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>LAMPlights &#187; SQL</title>
	<atom:link href="http://www.hermanradtke.com/blog/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hermanradtke.com</link>
	<description>Personal anecdotes from my experiences using the LAMP stack</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:14:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ORM frameworks are black magic</title>
		<link>http://www.hermanradtke.com/blog/orm-frameworks-are-black-magic/</link>
		<comments>http://www.hermanradtke.com/blog/orm-frameworks-are-black-magic/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 22:01:58 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[pdo]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/?p=806</guid>
		<description><![CDATA[I read a really good post from Vic on his move away from ORM frameworks. I did not agree with everything he said though and wanted to start a discussion. Unfortunately, there is no way to leave comments on his blog. The next best thing is to post it here. Like Vic, I too have [...]]]></description>
			<content:encoded><![CDATA[<p>I read a really good post from <a title="The Last PHP PDO Library You Will Ever Need " href="http://leftnode.com/entry/the-last-php-pdo-library-you-will-ever-need" target="_blank">Vic on his move away from ORM frameworks</a>. I did not agree with everything he said though and wanted to start a discussion. Unfortunately, there is no way to leave comments on his blog. The next best thing is to post it here.</p>
<p><span id="more-806"></span>Like Vic, I too have moved away from using ORM frameworks. One of my biggest problems with ORM frameworks is that they become a de-facto language within themselves. Instead of learning how to write proper SQL according to the SQL-99 standard, I now have to learn SQL and the syntax of the ORM. Things only get worse from there when trying to debug problems of performance and correctness.</p>
<p>There are some niche tasks where I still use an ORM despite the costs mentioned by Vic and myself. An ORM is great for creating dynamic queries. As we build more rich front-end web applications, it is very easy to build queries with user specified filters and sorting. Trying to do this manually is time-consuming and often bug prone. I find this setup particularly useful when building administrative pages that are looking at lists of things like users or orders. Performance is not a real big issue here and the queries themselves are usually only joining one or two tables. There is still the risk of a runaway query wreaking havoc on your master database. To combat this scare, I advocate a separate slave database to run queries like this.</p>
<p>Vic mentioned that he puts the SQL directory in the controllers, but I think this is a mistake. Controllers are tying the view to the model and should not contain any other logic. I prefer to keep business logic at the library level and data access logic in the model. The controller can consume library or model classes at will and pass them over to the view for output. This may seem like an extra step that is not needed, but it makes testing much easier. There is also the added benefit of being able to write a service layer or API with very little effort should the need arise.</p>
<p>All in all I think Vic makes a good argument. I think developers need to learn to embrace databases, SQL or NoSQL, instead of trying to abstract them away. Data is often the most important part of the business and it should be treated as such.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/orm-frameworks-are-black-magic/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Crutching on MySQL&#8217;s INSERT IGNORE</title>
		<link>http://www.hermanradtke.com/blog/crutching-on-mysqls-insert-ignore/</link>
		<comments>http://www.hermanradtke.com/blog/crutching-on-mysqls-insert-ignore/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 04:42:36 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=17</guid>
		<description><![CDATA[MySQL has a lot of nice additions to the SQL-92 standard that make life easier for developers. The drawback to these shortcuts is that many developers don't learn how to cope in other database environments, like PostgreSQL. Take INSERT IGNORE for example: PostgreSQL does not have support for this syntax. A resourceful developer can do [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL has a lot of nice additions to the SQL-92 standard that make life easier for developers.  The drawback to these shortcuts is that many developers don't learn how to cope in other database environments, like PostgreSQL.  Take INSERT IGNORE for example:  PostgreSQL does not have support for this syntax.  A resourceful developer can do a quick google search to learn an alternative, but many of the solutions at the top of google will show the wrong way to solve this problem.</p>
<p><span id="more-17"></span></p>
<p>Common bad example 1: Use a SELECT statement to check if the key exists and INSERT only if the key is not found.<br />
The main problem with this solution is the glaring race condition.  In the time in between the SELECT and INSERT, another session can insert the record.  You also have to write extra code into your application to process the SELECT query, which may have to specific for many cases.  A big waste of time.</p>
<p>Common bad example 2: Delete the row containing the key to be inserted and then INSERT the data again.<br />
Hopefully this is done in a transaction, but it still is dicey.  While this solution requires no extra application code to be written, it will only work for very basic cases.  Timestamps, flags and other meta data will be lost with a blind delete.  This solution scares me the most.</p>
<p>Both examples 1 and 2 can also be made into stored procedures.  Don't be tricked into thinking this is a better solution.</p>
<p><strong>The best solution</strong> is to insert the data and, if there is an error, check the error code.  This is basically what the MySQL INSERT IGNORE syntax does!  PostgreSQL, and most major relational databases, follow the ANSI SQL-92 standard.  The SQL-92 standard contains a clear list of error codes that an application can check against.  If there is a duplicate key error, the application can handle it accordingly.  This does not require an extra query, has no race condition and is easy to check for in the application.</p>
<p>For those wondering, 23505 is the error code for duplicate key errors.</p>
<p>A list of all the SQL error codes can be found here:<br />
<a href="http://db.apache.org/derby/docs/10.3/ref/rrefexcept71493.html">http://db.apache.org/derby/docs/10.3/ref/rrefexcept71493.html</a></p>
<p>Edit: Maggie Nelson talks about her experience with this very same issue from an Oracle users point of view in <a href="http://maggienelson.com/2009/03/the-rules-of-software-engagement/">The Rules of (Software) Engagement</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/crutching-on-mysqls-insert-ignore/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

