<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Adam Machanic : puzzle, Performance</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/puzzle/Performance/default.aspx</link><description>Tags: puzzle, Performance</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>SQL Server Query Processing Puzzle: LIKE vs ?</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/04/22/sql-server-query-processing-puzzle-like-vs.aspx</link><pubDate>Tue, 22 Apr 2008 14:18:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6344</guid><dc:creator>Adam Machanic</dc:creator><slash:comments>33</slash:comments><comments>http://www2.sqlblog.com/blogs/adam_machanic/comments/6344.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/adam_machanic/commentrss.aspx?PostID=6344</wfw:commentRss><description>&lt;p&gt;How creative are you with manipulating your queries to produce more efficient plans? Try the following puzzle and e-mail your solution to me at [&amp;lt;do_not_mail&amp;gt; @ do_not_mail.com].  Make sure to include an explanation of why it works, as well as your mailing address. The best two solutions/explanations win a free copy of &lt;a href="http://www.amazon.com/dp/159059729X"&gt;Expert SQL Server 2005 Development&lt;/a&gt;, a wonderful feeling of accomplishment, plus eternal fame and glory when I reveal your solutions here on the blog.

&lt;/p&gt;&lt;p&gt;Run the following T-SQL to create two tables in TempDB:
&lt;/p&gt;&lt;pre style="margin-left:40px;"&gt;USE TempDB&lt;br&gt;GO&lt;br&gt;&lt;br&gt;CREATE TABLE b1 (blat1 CHAR(5) NOT NULL)&lt;br&gt;CREATE TABLE b2 (blat2 VARCHAR(200) NOT NULL)&lt;br&gt;GO&lt;br&gt;&lt;br&gt;INSERT b1&lt;br&gt;SELECT LEFT(AddressLine1, 5) AS blat1&lt;br&gt;FROM AdventureWorks.Person.Address&lt;br&gt;&lt;br&gt;INSERT b2&lt;br&gt;SELECT AddressLine1 AS blat2&lt;br&gt;FROM AdventureWorks.Person.Address&lt;br&gt;GO&lt;/pre&gt;
Now consider the following query:
&lt;pre style="margin-left:40px;"&gt;SELECT *&lt;br&gt;FROM b1&lt;br&gt;JOIN b2 ON&lt;br&gt;    b2.blat2 LIKE b1.blat1 + '%'&lt;/pre&gt;&lt;p&gt;
This query takes around three minutes to run on my notebook, and does over 1.8 million logical reads.  Can you figure out a way to re-write it so that it performs better?  No modification of the base tables or addition of any other objects is allowed (sorry, no indexed views!) -- the challenge is to tune this by doing nothing more than re-writing the query.

&lt;/p&gt;&lt;p&gt;Good luck!  I'll leave the contest open for submissions until May 1.&lt;/p&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=6344" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/Performance/default.aspx">Performance</category><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/Query+Tuning/default.aspx">Query Tuning</category><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/puzzle/default.aspx">puzzle</category></item></channel></rss>