<?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>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><description>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</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: 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#6347</link><pubDate>Tue, 22 Apr 2008 17:05:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6347</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;To people responding: Don't just send me a solution. &amp;nbsp;Explain to me WHY it works. &amp;nbsp;Why does your change allow the query processor to work more efficiently? &amp;nbsp;The goal of this challenge is not just to modify a query and happen upon a faster version, but rather to think about the reasons behind it and gain a deeper understanding of the QP. &amp;nbsp;Enjoy!&lt;/p&gt;
</description></item><item><title>re: 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#6358</link><pubDate>Wed, 23 Apr 2008 14:55:43 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6358</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Hi Paul,&lt;/p&gt;
&lt;p&gt;Why did you post your solution here instead of e-mailing it to Adam? Don't you think it might have skewed the contest just a little bit?&lt;/p&gt;
</description></item><item><title>re: 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#6360</link><pubDate>Wed, 23 Apr 2008 15:37:58 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6360</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Paul D., next time think before you post.&lt;/p&gt;
</description></item><item><title>re: 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#6362</link><pubDate>Wed, 23 Apr 2008 18:17:05 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6362</guid><dc:creator>david wei</dc:creator><description>&lt;p&gt;Adam, do you think change blat1 to VARCHAR(5) will make this more challenging? &amp;nbsp;People may take the advantage of CHAR(5). :-)&lt;/p&gt;
</description></item><item><title>re: 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#6363</link><pubDate>Wed, 23 Apr 2008 19:05:40 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6363</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;David:&lt;/p&gt;
&lt;p&gt;If you want more of a challenge, try changing all of the 5s to 7s ... I'm not sure if this will be more or less challenging than the VARCHAR(5) change; I'll let you tell me :-)&lt;/p&gt;
</description></item><item><title>re: 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#6365</link><pubDate>Wed, 23 Apr 2008 19:51:47 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6365</guid><dc:creator>david wei</dc:creator><description>&lt;p&gt;Adam, not the length issue, is the trailing space issue.&lt;/p&gt;
&lt;p&gt;Trailing space makes the difference.&lt;/p&gt;
&lt;p&gt;for example:&lt;/p&gt;
&lt;p&gt;if 'abcde' like 'a%'&lt;/p&gt;
&lt;p&gt; print 'Y' -- returns Y&lt;/p&gt;
&lt;p&gt;if 'abcde' like 'a &amp;nbsp;%'&lt;/p&gt;
&lt;p&gt; print 'Yes' -- does not return Yes&lt;/p&gt;
&lt;p&gt;So if we know the blat1 is fixed 5 characters, we can simply use the left(balt2,5) = balt1 for the join, and a simple HASH join hint will only sacn two tables once, avoid nested loop joins on two 19K rows tables(which caused 1.8M reads).&lt;/p&gt;
&lt;p&gt;But if we change blat1 to VARCHAR(5), and truncate the trailing space to make the length vary (from 1 to 5), this will add the chalenge, hence you can not just compare the first 5 characters :-)&lt;/p&gt;
&lt;p&gt;INSERT b1&lt;/p&gt;
&lt;p&gt;SELECT RTRIM(LEFT(AddressLine1, 5)) AS blat1&lt;/p&gt;
&lt;p&gt;FROM AdventureWorks.Person.Address&lt;/p&gt;
&lt;p&gt;-- Note. you can comment out some sensitive part in my post if you think this will skew the contest.&lt;/p&gt;
</description></item><item><title>re: 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#6366</link><pubDate>Wed, 23 Apr 2008 20:07:15 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6366</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;David,&lt;/p&gt;
&lt;p&gt;Length is an issue here as well, due to some of the data. &amp;nbsp;Try creating everything with length 7, and play with some of the solutions (assuming you've come up with one -- I don't see a submission from you in my inbox). &amp;nbsp;If your solution happens to be the most common one people have been sending, you'll find that it will no longer return the same results as the example query.&lt;/p&gt;
</description></item><item><title>re: 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#6367</link><pubDate>Wed, 23 Apr 2008 20:21:27 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6367</guid><dc:creator>Denis Gobo</dc:creator><description>&lt;p&gt;David, that didn't make a difference, I am getting the same number of rows back&lt;/p&gt;
&lt;p&gt;SELECT blat1,len(blat1),datalength(blat1),b1.blat1 ,left(b2.blat2,5), &lt;/p&gt;
&lt;p&gt;len(blat2),datalength(blat2)&lt;/p&gt;
&lt;p&gt;FROM b1&lt;/p&gt;
&lt;p&gt;JOIN b2 ON &amp;nbsp;&amp;lt;very secret indeed&amp;gt;&lt;/p&gt;
&lt;p&gt;len and datalength reveal that there are some rows with 5 and 4 chars&lt;/p&gt;
</description></item><item><title>re: 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#6368</link><pubDate>Wed, 23 Apr 2008 20:24:32 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6368</guid><dc:creator>david wei</dc:creator><description>&lt;p&gt;Adam, still the trailing space issue, because the minimal length in b2 is 5, not 7. &lt;/p&gt;
&lt;p&gt;select min(len((blat2))) from b2&lt;/p&gt;
&lt;p&gt;So when you change to 7, blat1 automatic added 2 trailing space to make it CHAR(7); that cause result difference.&lt;/p&gt;
&lt;p&gt;I have sent my solution.&lt;/p&gt;
</description></item><item><title>re: 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#6370</link><pubDate>Wed, 23 Apr 2008 20:36:02 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6370</guid><dc:creator>Michelle</dc:creator><description>&lt;p&gt;So Adam,&lt;/p&gt;
&lt;p&gt;Do we need to resend you a new solution using CHAR(7), or do we still have a chance to win...? &amp;nbsp; :-)&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Michelle.&lt;/p&gt;
</description></item><item><title>re: 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#6371</link><pubDate>Wed, 23 Apr 2008 20:44:40 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6371</guid><dc:creator>david wei</dc:creator><description>&lt;p&gt;Denis, it does make the difference. The len() does not count the trailing space, but they affect the like operator. &lt;/p&gt;
&lt;p&gt;Try create the b3 table use trimmed varchar(5) as below: &lt;/p&gt;
&lt;p&gt;(CREATE TABLE b3 (blat3 VARCHAR(5) NOT NULL)&lt;/p&gt;
&lt;p&gt;INSERT b3&lt;/p&gt;
&lt;p&gt;SELECT RTRIM(LEFT(AddressLine1, 5)) AS blat1&lt;/p&gt;
&lt;p&gt;FROM AdventureWorks.Person.Address&lt;/p&gt;
&lt;p&gt;SELECT count(*) FROM b3 inner JOIN b2 ON &amp;nbsp; &amp;nbsp;b2.blat2 LIKE b3.blat3 + '%'&lt;/p&gt;
&lt;p&gt;--Trimmed VARCHAR(5) returns 111181 rows&lt;/p&gt;
&lt;p&gt;SELECT count(*) FROM b1 inner JOIN b2 ON &amp;nbsp; &amp;nbsp;b2.blat2 LIKE b1.blat1 + '%'&lt;/p&gt;
&lt;p&gt;-- original CAHR(5) returns 109984 rows&lt;/p&gt;
</description></item><item><title>re: 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#6373</link><pubDate>Wed, 23 Apr 2008 20:51:28 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6373</guid><dc:creator>Denis Gobo</dc:creator><description>&lt;p&gt;David,&lt;/p&gt;
&lt;p&gt;I see I was talking about LEFT not LIKE, with LEFT it doesn't matter if it is char or varchar&lt;/p&gt;
</description></item><item><title>re: 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#6412</link><pubDate>Fri, 25 Apr 2008 09:51:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6412</guid><dc:creator>Fabiano Neves Amorim</dc:creator><description>&lt;p&gt;Umm, Denis, I think with the answer you &amp;quot;spilled your beans&amp;quot; about your secret :-(&lt;/p&gt;
</description></item><item><title>re: 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#6577</link><pubDate>Fri, 02 May 2008 05:23:47 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6577</guid><dc:creator>Gordon</dc:creator><description>&lt;p&gt;It's all about iteration.&lt;/p&gt;
</description></item><item><title>re: 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#6597</link><pubDate>Fri, 02 May 2008 16:35:44 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6597</guid><dc:creator>Igor Mikhalyev</dc:creator><description>&lt;p&gt;So where are three best queries? &amp;nbsp;Who are the winners?&lt;/p&gt;
</description></item><item><title>re: 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#6630</link><pubDate>Mon, 05 May 2008 16:14:08 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6630</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Igor: Patience, please. &amp;nbsp;I got a -lot- of responses... Trying to figure out who won now.&lt;/p&gt;
</description></item><item><title>re: 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#6742</link><pubDate>Fri, 09 May 2008 21:23:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:6742</guid><dc:creator>too embarrassed to say</dc:creator><description>&lt;p&gt;Criminy. I didn't even NOTICE the char vs. varchar part. Now my answer could be wrong. Blah.&lt;/p&gt;
</description></item><item><title>re: 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#7125</link><pubDate>Tue, 03 Jun 2008 21:55:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:7125</guid><dc:creator>Igor Mikhalyev</dc:creator><description>&lt;p&gt;So where are three best queries? &amp;nbsp;Who are the winners?&lt;/p&gt;
</description></item><item><title>re: 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#7147</link><pubDate>Thu, 05 Jun 2008 11:24:16 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:7147</guid><dc:creator>Magneto</dc:creator><description>&lt;p&gt;We are waiting .......&lt;/p&gt;
</description></item><item><title>re: 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#7274</link><pubDate>Fri, 13 Jun 2008 02:23:03 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:7274</guid><dc:creator>Jonathan Kehayias</dc:creator><description>&lt;p&gt;If you follow this blog, you should understand that Adam has been both busy, and experienced a major setback like the drive failure. &amp;nbsp;If you don't speak publicly, I would recommend giving it a shot, and then you will understand just how much time is dedicated to doing a meaningful presentation. &amp;nbsp;I too was curious about the answers and emailed Adam a few weeks back, and he responded that he is trying to work this out, but he had to start over after the drive crash.&lt;/p&gt;
</description></item><item><title>re: 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#7343</link><pubDate>Tue, 17 Jun 2008 15:49:45 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:7343</guid><dc:creator>BPhilip</dc:creator><description>&lt;p&gt;set &amp;nbsp;statistics io on&lt;/p&gt;
&lt;p&gt;SELECT *FROM b1 inner JOIN b2 ON &amp;nbsp; &amp;nbsp;left(b2.blat2,5) = b1.blat1 &lt;/p&gt;
&lt;p&gt;I used this query and got results in 2 seconds. Following are my scan results.&lt;/p&gt;
&lt;p&gt;(109984 row(s) affected)&lt;/p&gt;
&lt;p&gt;Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;/p&gt;
&lt;p&gt;Table 'b2'. Scan count 1, logical reads 74, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;/p&gt;
&lt;p&gt;Table 'b1'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;/p&gt;
</description></item><item><title>re: 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#7386</link><pubDate>Thu, 19 Jun 2008 10:43:18 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:7386</guid><dc:creator>Andrew</dc:creator><description>&lt;p&gt;It would be very interesting to see the top performing query...&lt;/p&gt;
</description></item><item><title>Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/04/22/sql-server-query-processing-puzzle-like-vs.aspx#9165</link><pubDate>Wed, 01 Oct 2008 19:43:04 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9165</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;In late April, I posted a puzzle to test readers' knowledge of SQL Server query processing internals&lt;/p&gt;
</description></item><item><title>A year in review, The 31 best blog posts on SQLBlog for 2008</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/04/22/sql-server-query-processing-puzzle-like-vs.aspx#10858</link><pubDate>Wed, 31 Dec 2008 15:37:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10858</guid><dc:creator>Denis Gobo</dc:creator><description>&lt;p&gt;Wow, it has been already a year since I wrote A year in review, The 21 + 1 best blog posts on SQLBlog&lt;/p&gt;
</description></item><item><title>re: 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#11701</link><pubDate>Tue, 03 Feb 2009 18:19:03 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11701</guid><dc:creator>gyanendra</dc:creator><description>&lt;p&gt;how we can make like query and in like condition i want to give a hindi text which are exist in the database collumn. and the collumn type is nvarchar.&lt;/p&gt;
</description></item><item><title>re: 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#11961</link><pubDate>Wed, 18 Feb 2009 00:12:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11961</guid><dc:creator>NotNowJohn</dc:creator><description>&lt;p&gt;Since the competition is already finished I would post here my explanation. &lt;/p&gt;
&lt;p&gt;The execution plan of the query shows that nested loop join has been used. The reason why is simple - only this join operator is possible without an equijoin predicat. The solution for this problem would be to force usage of hash join operator and it requires at least one equijoin predicat. Simple transformation LIKE =&amp;gt; LEFT solves this problem and hash join is efficient in this scenario.&lt;/p&gt;
&lt;p&gt;BTW. the estimated plan for the query&lt;/p&gt;
&lt;p&gt;SELECT * FROM b1&lt;/p&gt;
&lt;p&gt;JOIN b2 ON b2.blat2 &amp;gt; b1.blat1 &lt;/p&gt;
&lt;p&gt;says that this estimated number of rows for the nested loops would be more than 85M!&lt;/p&gt;
</description></item><item><title>T-SQL Challenge: Grouped String Concatenation</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/04/22/sql-server-query-processing-puzzle-like-vs.aspx#12308</link><pubDate>Fri, 27 Feb 2009 18:24:54 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12308</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;It's been quite a while since the LIKE vs ? Puzzle , and I feel like it's time for another one. Response&lt;/p&gt;
</description></item><item><title>re: 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#12396</link><pubDate>Mon, 02 Mar 2009 23:06:43 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12396</guid><dc:creator>SqlGuru</dc:creator><description>&lt;p&gt;Please try this its 3sec&lt;/p&gt;
&lt;p&gt;select * from b1&lt;/p&gt;
&lt;p&gt;inner join b2 &lt;/p&gt;
&lt;p&gt;on b1.blat1=substring(b2.blat2,1,5)&lt;/p&gt;
</description></item><item><title>re: 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#12397</link><pubDate>Mon, 02 Mar 2009 23:07:49 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12397</guid><dc:creator>SqlGuru</dc:creator><description>&lt;p&gt;typo mistake sorry its 1sec&lt;/p&gt;
</description></item><item><title>re: 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#12457</link><pubDate>Sat, 07 Mar 2009 08:48:16 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12457</guid><dc:creator>Vimvq1987</dc:creator><description>&lt;p&gt;SELECT DISTINCT *&lt;/p&gt;
&lt;p&gt;FROM b1&lt;/p&gt;
&lt;p&gt;JOIN b2 ON&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;--b2.blat2 LIKE b1.blat1 + '%'&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;b1.blat1 = LEFT(b2.blat2,5)&lt;/p&gt;
&lt;p&gt;Sorry, I'm late. I just know this blog today. After 10 minutes trying to figure out what really happened inside the query, I found something:&lt;/p&gt;
&lt;p&gt;JOIN (or Inner Join) return matching common values between two tables. So, in the original query, SQL Server has to read a row in the b1 table, and then reads all rows in the b2 table to find matching strings, and then, repeats until end of table b1. LIKE Operator is relatively slow, so It'll take a long time to execute the query (About 1m41s in my computer :P.)&lt;/p&gt;
&lt;p&gt;One way to improve performance is replace LIKE operator with operator =. We already knew that blat1 column contains only CHAR(5) string, so that, Join condition can be rewrite like this:&lt;/p&gt;
&lt;p&gt;b1.blat1 = LEFT(b2.blat2,5)&lt;/p&gt;
&lt;p&gt;Even with LEFT function, this new query outperform the old one, it costs about 2s to run in my computer, but still affects 109984 rows, equals to the old one.&lt;/p&gt;
&lt;p&gt;The fact is, when we insert values into blat1 column, it has been trimmed into CHAR(5). In example, the following addresses make sense in blat2 column, but they don't make any in blat1 column.&lt;/p&gt;
&lt;p&gt;1, place Beaubernard&lt;/p&gt;
&lt;p&gt;1, place de Brazaville&lt;/p&gt;
&lt;p&gt;1, place de la R&amp;#233;publique&lt;/p&gt;
&lt;p&gt;They'll be inserted into blat1 with the same value as '1, pl',and when executing the query, SQL server has to repeat Join operator many times to same values that are exactly same. So I added DISTINCT keyword to prevent it from wasting time. Now my query returns only 13567 rows and takes only near 0s to run.&lt;/p&gt;
&lt;p&gt;Hope this right!&lt;/p&gt;
</description></item><item><title>re: 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#15413</link><pubDate>Wed, 22 Jul 2009 13:57:46 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:15413</guid><dc:creator>Brian Tkatch</dc:creator><description>&lt;p&gt;Just saw this now. So i'll post:&lt;/p&gt;
&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM b1&lt;/p&gt;
&lt;p&gt;WHERE b1.blat1 IN (SELECT LEFT(b2.blat2, 5) FROM b2);&lt;/p&gt;
&lt;p&gt;It is faster because it can do 1 read on b2. The LIKE doesn't know that b1 is five chars and therefor, it has to re-evaluate the expression in b2 for each record in b1. When IN is used, it generates a sub-table first.&lt;/p&gt;
&lt;p&gt;That my guess at least. :)&lt;/p&gt;
</description></item><item><title>re: 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#26314</link><pubDate>Mon, 21 Jun 2010 06:25:07 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26314</guid><dc:creator>Divya</dc:creator><description>&lt;p&gt;Try this&lt;/p&gt;
&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM b1&lt;/p&gt;
&lt;p&gt;JOIN b2 ON&lt;/p&gt;
&lt;p&gt;LEFT(b2.blat2,5) =b1.blat1&lt;/p&gt;
</description></item><item><title>re: 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#35389</link><pubDate>Mon, 02 May 2011 12:50:41 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:35389</guid><dc:creator>Olivier Comte</dc:creator><description>&lt;p&gt;These solutions aren't equivalent to (SELECT *FROM b1JOIN b2 ON &amp;nbsp; &amp;nbsp;b2.blat2 LIKE b1.blat1 + '%') if b1.blat1 contain a &amp;quot;special&amp;quot; character like '%'.&lt;/p&gt;
&lt;p&gt;For example, if there is one record in b1 with blat1='%a',and a record in b2 with blat2='a',&lt;/p&gt;
&lt;p&gt;SELECT * FROM b1 JOIN b2 ON &amp;nbsp; &amp;nbsp;b2.blat2 LIKE b1.blat1 + '%'&lt;/p&gt;
&lt;p&gt;will return this record but&lt;/p&gt;
&lt;p&gt;SELECT * FROM b1 JOIN b2 ON LEFT(b2.blat2,5) =b1.blat1&lt;/p&gt;
&lt;p&gt;won't.&lt;/p&gt;
</description></item></channel></rss>