<?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>Pramatr Blog &#187; Bugs</title>
	<atom:link href="http://www.pramatr.com/blog/tag/bugs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pramatr.com/blog</link>
	<description>A collection of articles from pramatr.com on technology, security, software and anything we find interesting</description>
	<lastBuildDate>Mon, 29 Mar 2010 19:48:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SQL n + 1 Selects Explained</title>
		<link>http://www.pramatr.com/blog/2009/02/05/sql-n-1-selects-explained/</link>
		<comments>http://www.pramatr.com/blog/2009/02/05/sql-n-1-selects-explained/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 06:00:47 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Improvement]]></category>
		<category><![CDATA[Jdbc]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Smells]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=426</guid>
		<description><![CDATA[The SQL n + 1 selects problem is extremely common but I have often found that many people have either never heard of it or simply don&#8217;t understand it. It is actually very easy to introduce a problem like this into your code, but it&#8217;s also very easy to resolve as well. Problems like this [...]]]></description>
			<content:encoded><![CDATA[<p>The SQL n + 1 selects problem is extremely common but I have often found that many people have either never heard of it or simply don&#8217;t understand it. It is actually very easy to introduce a problem like this into your code, but it&#8217;s also very easy to resolve as well. Problems like this are best explained with an example; so imagine we have a table called users and another called user_roles. These tables are setup with a one-to-many relationship, meaning that one user (e.g. jsmith) can have many roles (e.g. Administrator, Auditor, Developer). Many people might implement something like this;</p>
<blockquote>
<pre><span style="font-size:small;"><strong>public</strong> <span style="color:#2040a0;">Iterable</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span> <span style="color:#2040a0;">allUsers</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
    <strong>final</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">selectUsers</span> <span style="color:#4444ff;">=</span> <span style="color:#008000;">"select users.username, users.email, " +</span>
        <span style="color:#008000;">"users.last_password_change from users"</span><span style="color:#4444ff;">;</span>
    <strong>return</strong> <span style="color:#2040a0;">getJdbcTemplate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span>.<span style="color:#2040a0;">query</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">selectUsers</span>, <strong>new</strong> <span style="color:#2040a0;">Object</span><span style="color:#4444ff;"><strong>[</strong></span><span style="color:#4444ff;"><strong>]</strong></span> <span style="color:#4444ff;"><strong>{</strong></span><span style="color:#4444ff;"><strong>}</strong></span>,
                <strong>new</strong> <span style="color:#2040a0;">ParameterizedRowMapper</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
        <strong>public</strong> <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">mapRow</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">ResultSet</span> <span style="color:#2040a0;">resultSet</span>, <strong>int</strong> <span style="color:#2040a0;">rowNumber</span><span style="color:#4444ff;"><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">SQLException</span> <span style="color:#4444ff;"><strong>{</strong></span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">username</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"username"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">email</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"email"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">Date</span> <span style="color:#2040a0;">lastPasswordChange</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getDate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"last_password_change"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span> <span style="color:#4444ff;">=</span> <strong>new</strong> <span style="color:#2040a0;">DefaultUser</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span>, <span style="color:#2040a0;">email</span>, <span style="color:#2040a0;">lastPasswordChange</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">addRolesToUser</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">user</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <strong>return</strong> <span style="color:#2040a0;">user</span><span style="color:#4444ff;">;</span>
        <span style="color:#4444ff;"><strong>}</strong></span>
    <span style="color:#4444ff;"><strong>}</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
<span style="color:#4444ff;"><strong>}</strong></span>

<strong>private</strong> <strong>void</strong> <span style="color:#2040a0;">addRolesToUser</span><span style="color:#4444ff;"><strong>(</strong></span><strong>final</strong> <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
    <strong>final</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">selectUserRoles</span> <span style="color:#4444ff;">=</span> <span style="color:#008000;">"select role_name from user_roles where username = ?"</span><span style="color:#4444ff;">;</span>
    <span style="color:#2040a0;">getJdbcTemplate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span>.<span style="color:#2040a0;">query</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">selectUserRoles</span>, <strong>new</strong> <span style="color:#2040a0;">Object</span><span style="color:#4444ff;"><strong>[</strong></span><span style="color:#4444ff;"><strong>]</strong></span> <span style="color:#4444ff;"><strong>{</strong></span> <span style="color:#2040a0;">user</span>.<span style="color:#2040a0;">getPrincipalName</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>}</strong></span>,
                <strong>new</strong> <span style="color:#2040a0;">RowCallbackHandler</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
        <strong>public</strong> <strong>void</strong> <span style="color:#2040a0;">processRow</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">ResultSet</span> <span style="color:#2040a0;">resultSet</span><span style="color:#4444ff;"><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">SQLException</span> <span style="color:#4444ff;"><strong>{</strong></span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">rolename</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"role_name"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">user</span>.<span style="color:#2040a0;">addRole</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">rolename</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
        <span style="color:#4444ff;"><strong>}</strong></span>
    <span style="color:#4444ff;"><strong>}</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
<span style="color:#4444ff;"><strong>}</strong></span></span></pre>
</blockquote>
<p>Reviewing the code we can see one query is executed to retrieve the users, the problem here is for each user another SQL statement needs to be executed to retrieve the roles. If the first query retrieved one user, this would require one additional query to retrieve the roles. If the first query retrieved a hundred users, this would require one hundred additional queries to retrieve the roles. The pattern will always be the same, one query for the users and n queries dependent on the number of users found, thus n + 1. Although this solution is functional, it does result in many unnecessary SQL statements being executed.</p>
<blockquote>
<pre><span style="font-size:small;"><strong>select</strong> users.username, users.email, users.last_password_change <strong>from</strong> users;
<strong>select</strong> role_name <strong>from</strong> user_roles <strong>where</strong> username = ?;
<strong>select</strong> role_name <strong>from</strong> user_roles <strong>where</strong> username = ?;
<strong>select</strong> role_name <strong>from</strong> user_roles <strong>where</strong> username = ?;
...</span></pre>
</blockquote>
<p>Shared resources are typically the bottleneck in most applications, so expensive or unnecessary SQL should be avoided if possible. As the application attempts to scale, this bottleneck can become extremely problematic and severely inhibit application performance. Fortunately this is a simple solutions to this problem; introducing a join into the query.</p>
<blockquote>
<pre><span style="font-size:small;"><strong>public</strong> <span style="color:#2040a0;">Iterable</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span> <span style="color:#2040a0;">allUsers</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
    <strong>final</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">selectUsers</span> <span style="color:#4444ff;">=</span>
        <span style="color:#008000;">"select users.username, users.email, users.last_password_change, user_roles.role_name "</span>
            <span style="color:#4444ff;">+</span> <span style="color:#008000;">"from users left join user_roles on (users.username = user_roles.username)"</span><span style="color:#4444ff;">;</span>
    <strong>final</strong> <span style="color:#2040a0;">Map</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">String</span>, <span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span> <span style="color:#2040a0;">users</span> <span style="color:#4444ff;">=</span> <strong>new</strong> <span style="color:#2040a0;">HashMap</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">String</span>, <span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
    <span style="color:#2040a0;">getJdbcTemplate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span>.<span style="color:#2040a0;">query</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">selectUsers</span>, <strong>new</strong> <span style="color:#2040a0;">Object</span><span style="color:#4444ff;"><strong>[</strong></span><span style="color:#4444ff;"><strong>]</strong></span> <span style="color:#4444ff;"><strong>{</strong></span><span style="color:#4444ff;"><strong>}</strong></span>, <strong>new</strong> <span style="color:#2040a0;">RowCallbackHandler</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
        <strong>public</strong> <strong>void</strong> <span style="color:#2040a0;">processRow</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">ResultSet</span> <span style="color:#2040a0;">resultSet</span><span style="color:#4444ff;"><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">SQLException</span> <span style="color:#4444ff;"><strong>{</strong></span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">username</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"username"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <strong>if</strong> <span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;">!</span><span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">containsKey</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
                <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">email</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"email"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">Date</span> <span style="color:#2040a0;">lastPasswordChange</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getDate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"last_password_change"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span> <span style="color:#4444ff;">=</span> <strong>new</strong> <span style="color:#2040a0;">DefaultUser</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span>, <span style="color:#2040a0;">email</span>, <span style="color:#2040a0;">lastPasswordChange</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">put</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span>, <span style="color:#2040a0;">user</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#4444ff;"><strong>}</strong></span>

            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">rolename</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"role_name"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <strong>if</strong> <span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;">!</span><span style="color:#2040a0;">StringUtil</span>.<span style="color:#2040a0;">isNull</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">rolename</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
                <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">get</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">user</span>.<span style="color:#2040a0;">addRole</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">rolename</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#4444ff;"><strong>}</strong></span>
        <span style="color:#4444ff;"><strong>}</strong></span>
    <span style="color:#4444ff;"><strong>}</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
    <strong>return</strong> <span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">values</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
<span style="color:#4444ff;"><strong>}</strong></span></span></pre>
</blockquote>
<p>Although the code and SQL statement are slightly more complex that the original example, it results in much fewer SQL statements being executed. Instead of the n + 1 statements executed in the first example, one statement is executed that fetches all the required data. This typically results in much improved performance and as the numbers scale the improvement in performance can become much more apparent.</p>
<blockquote>
<pre><span style="font-size:small;"><strong>select</strong> users.username, users.email, users.last_password_change, user_roles.role_name
    <strong>from</strong> users left join user_roles <strong>on</strong> <strong>(</strong>users.username = user_roles.username);</span></pre>
</blockquote>
<p>As with all performance optimizations the most important thing is to measure the effect of the improvement. Performance optimizations aren&#8217;t always predictable so by taking measurements before and after the change, you can accurately know if you have actually improved the performance (or made it worse). A SQL join may be the most appropriate way of solving a problem such as this, but there are other alternatives such as <a href="http://ehcache.sourceforge.net/">caching</a> the data instead. Although the SQL n + 1 selects is an extremely common problem, is not always well understood and is often still found within code. It is very easy to introduce a problem like this into your code, but it&#8217;s also very easy to resolve as well. Next time you are viewing your debug output, see if you can spot SQL n + 1 selects.</p>
<p><strong>References</strong></p>
<p>Database access using <a href="http://pramatr.com/2008/08/19/spring-jdbctemplate-the-phantom-performance-problem/">Spring JdbcTemplate</a><br />
Preventing the n + 1 select problem when using <a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/performance.html#performance-fetching-custom">Hibernate</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/02/05/sql-n-1-selects-explained/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Google Reports The Entire Internet As Malware</title>
		<link>http://www.pramatr.com/blog/2009/01/31/google-reports-the-entire-internet-as-malware/</link>
		<comments>http://www.pramatr.com/blog/2009/01/31/google-reports-the-entire-internet-as-malware/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 16:28:47 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=396</guid>
		<description><![CDATA[For about twenty minutes today google has been reporting every single website as malware. Any search within google returned the normal search results, but every result also included a report that &#8220;this site may harm your computer&#8220;. Attempting to click on the search result and progress to the actual website returns a warning page with [...]]]></description>
			<content:encoded><![CDATA[<p>For about twenty minutes today google has been reporting every single website as malware. Any search within google returned the normal search results, but every result also included a report that <em>&#8220;<a href="http://www.google.com/support/websearch/bin/answer.py?answer=45449&amp;topic=360&amp;hl=en&amp;ei=EGWESb6YMYaR-gbTu40o&amp;sa=X&amp;oi=malwarewarninglink&amp;resnum=1&amp;ct=help">this site may harm your computer</a>&#8220;</em>. Attempting to click on the search result and progress to the actual website returns a warning page with no possible way of clicking through to the resulting page. Google was effectively blocking every single search result from reaching it&#8217;s destination.</p>
<p><img src="http://pramatr.files.wordpress.com/2009/01/googlemalware1.jpg" alt="googlemalware" title="googlemalware" width="538" height="135" class="aligncenter size-full wp-image-401" /></p>
<p>I read a thorough <a href="http://www.codinghorror.com/blog/archives/000859.html">discussion</a> of the malware behaviour many months ago, but I don&#8217;t think this is quite what google were looking for when they implemented the feature. I&#8217;m sure there are a great many retailers who are cursing at potentially lost revenue and users who are now unsure if a website is harmful or not. False positives are never helpful to users who are unsure about their actions at the best of times. Thankfully, the problem seems to be sorting itself out and only seemed to last for twenty minutes. It&#8217;s not yet apparent whether the issue has been rectified or simply that the feature has been disabled altogether. It will be very interesting to see how this one is explained!</p>
<p><strong>Update:</strong> It seems the problem was <a href="http://news.bbc.co.uk/1/hi/technology/7862840.stm">bigger</a> than I thought.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/01/31/google-reports-the-entire-internet-as-malware/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
