<?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>daydreaming &#187; SQL</title>
	<atom:link href="http://n79.org/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://n79.org</link>
	<description></description>
	<lastBuildDate>Sat, 31 Jul 2010 08:58:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Optimizing an SQL LIKE Query</title>
		<link>http://n79.org/2006/03/07/optimizing-an-sql-like-query/</link>
		<comments>http://n79.org/2006/03/07/optimizing-an-sql-like-query/#comments</comments>
		<pubDate>Wed, 08 Mar 2006 03:58:20 +0000</pubDate>
		<dc:creator>nikhilb</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[dev]]></category>

		<guid isPermaLink="false">http://thoughts.n79.org/2006/03/07/optimizing-an-sql-like-query/</guid>
		<description><![CDATA[Carlos did a quick test earlier in the day on our Apache Derby backend and found that in a particular like query Derby was 23x slower than on a equality query over the same data set. Very often, databases do not optimize LIKE queries, partially because it is rare for them to support the types [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fn79.org%2F2006%2F03%2F07%2Foptimizing-an-sql-like-query%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fn79.org%2F2006%2F03%2F07%2Foptimizing-an-sql-like-query%2F&amp;source=nikibobb&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p><a title="Carlos Maltzahn" href="http://www.soe.ucsc.edu/~carlosm/">Carlos</a> did a quick test earlier in the day on our <a title="Apache Derby" href="http://db.apache.org/derby/">Apache Derby</a> backend and found that in a particular like query Derby was 23x slower than on a equality query over the same data set. Very often, databases do not optimize LIKE queries, partially because it is rare for them to support the types of indices such optimizations would require.</p>
<p>It took me a little while to find this <a target="_blank" title="Apache Derby LIKE Transformations" href="http://db.apache.org/derby/docs/10.0/manuals/tuning/perf96.html#HDRSII-TRANSFORM-37208">gem on LIKE Transformations  </a>in the Derby tuning manual. Basically the document covers some transformations which the Derby SQL parser goes through to make LIKE queries optimizable. For me, this solved the problem as it allowed me to rewrite my query into a form that was optimizable albeit with a slight loss of functionality in my application. Unfortunately, this is not the case for all LIKE queries. The transformations are quite obvious and easy to follow once you see them, so take a look if you are having similar performance problems.</p>
<blockquote><p><strong>Before:</strong></p>
<blockquote><p>SELECT checksum FROM tags WHERE tag LIKE &#8216;%element%&#8217;</p>
<p><strong>Gets parsed into &#8211;</strong>  SELECT checksum FROM tags WHERE tag LIKE &#8216;%element%&#8217;</p></blockquote>
<p><strong>After</strong></p>
<blockquote><p>SELECT checksum FROM tags WHERE tag LIKE &#8216;element%&#8217;</p>
<p><strong>Gets parsed into &#8211;</strong>  SELECT checksum FROM tags WHERE tag >= &#8216;element&#8217;   AND tag < 'elemenu'</p></blockquote>
</blockquote>
<p>These transformations are also potentially useful with databases other than Derby when LIKE queries are running slow, as you could do them manually on your statements to the fullest extent possible potentially eliminating LIKE altogether.</p>
]]></content:encoded>
			<wfw:commentRss>http://n79.org/2006/03/07/optimizing-an-sql-like-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.777 seconds -->
