<?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>Pearl Tech &#187; slow</title>
	<atom:link href="http://blog.pearltechnology.com/tag/slow/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pearltechnology.com</link>
	<description></description>
	<lastBuildDate>Thu, 05 Jan 2012 14:47:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>varchar(MAX) parameter slows down Stored Procedure execution?</title>
		<link>http://blog.pearltechnology.com/varcharmax-parameter-slows-down-stored-procedure-execution/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/varcharmax-parameter-slows-down-stored-procedure-execution/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 19:07:40 +0000</pubDate>
		<dc:creator>Geer</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[slow]]></category>
		<category><![CDATA[Stored Procedure]]></category>
		<category><![CDATA[varchar(MAX)]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=406</guid>
		<description><![CDATA[I was testing one of our web applications one day and happened to see a simple Stored Procedure took over a minute to complete.  The Stored Procedure only takes one parameter.  If I get rid of the parameter and directly run the SQL statement inside the Stored Procedure, it took less than a second to [...]]]></description>
			<content:encoded><![CDATA[<p>I was testing one of our web applications one day and happened to see a simple Stored Procedure took over a minute to complete.  The Stored Procedure only takes one parameter.  If I get rid of the parameter and directly run the SQL statement inside the Stored Procedure, it took less than a second to complete.  So, I bet the problem is about the parameter I am passing in the Stored Procedure.  I checked their Execution Plan.  The Execution Plan of the Stored Procedure is much more complex then the simple Query and it has a much higher estimated cost.</p>
<p>I did some research online, and people are saying that the Parameter Sniffing could be the cause.  I tried to avoid it according to this <a href="http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=1181" target="_blank">post</a>, but it didn&#8217;t make much difference as far as how long it takes for the Stored Procedure to complete.</p>
<p>After trying many different things, I finally found out the problem.  The Stored Procedure was dynamically created from user-defined query using SQL Server Management Object within our .NET web application.  We also allow users to define parameters (such as @Param1) in their Queries.  Since we don&#8217;t know the maximum length of the string parameter, we used varchar(MAX) for all string type parameters.  That&#8217;s the cause of this problem.  Once I changed the varchar(MAX) to varchar(200), the Stored Procedure started running much faster and the Execution Plan looks very similar to the simple Query now.  Problem solved.</p>
<p>Just for curiosity, I compared some other scenarios.  I found out that varchar(MAX) doesn&#8217;t always make a huge difference in performance.  The Stored Procedure I had problem with had a target table in a remote database (Linked Server) and the version of the remote SQL Server is 2000 where varchar(MAX) is not supported.  I am surprised that I didn&#8217;t get any exception when I run the query.  I am not sure what SQL Server does in the back end to make this unsupported data type to work, but apparently it slows down the query execution.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/varcharmax-parameter-slows-down-stored-procedure-execution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

