<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx</link><description>Top N costly query plans</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#9879035</link><pubDate>Fri, 21 Aug 2009 20:55:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9879035</guid><dc:creator>gqnelson</dc:creator><description>&lt;p&gt;I found if you change, you get all queries instead of just SELECT:&lt;/p&gt;
&lt;p&gt;N'//sql:Batch/sql:Statements/sql:StmtSimple[@StatementType = &amp;quot;SELECT&amp;quot;]'&lt;/p&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;p&gt; N'//sql:Batch/sql:Statements/sql:StmtSimple/sql:QueryPlan[1]/sql:RelOp[1]'&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9879035" width="1" height="1"&gt;</description></item><item><title>re: Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#9878943</link><pubDate>Fri, 21 Aug 2009 19:51:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9878943</guid><dc:creator>gqnelson</dc:creator><description>&lt;p&gt;This returns the top 25 SELECT statements. How would I change it to return all types of statements?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9878943" width="1" height="1"&gt;</description></item><item><title>re: Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#9878909</link><pubDate>Fri, 21 Aug 2009 19:30:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9878909</guid><dc:creator>gqnelson</dc:creator><description>&lt;p&gt;How do I see the database name that the query is executing against?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9878909" width="1" height="1"&gt;</description></item><item><title>re: Query to determine CONVERT_IMPLICIT operations from query plans...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#1991533</link><pubDate>Fri, 30 Mar 2007 02:33:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1991533</guid><dc:creator>SQL Server Engine Team</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt; You can get the SELECT statements that have an expression with CONVERT_IMPLICIT in many ways. Below is a query that uses the sys.dm_exec_query_plan and XQuery to get the information:&lt;/p&gt;
&lt;p&gt;with XMLNAMESPACES ('&lt;a rel="nofollow" target="_new" href="http://schemas.microsoft.com/sqlserver/2004/07/showplan"&gt;http://schemas.microsoft.com/sqlserver/2004/07/showplan&lt;/a&gt;' as sql)&lt;/p&gt;
&lt;p&gt;select qs.sql_handle, qs.plan_handle, qs.total_elapsed_time, qs.last_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time&lt;/p&gt;
&lt;p&gt;, db_name(qp.dbid) as database_name&lt;/p&gt;
&lt;p&gt;, quotename(object_schema_name(qp.objectid, qp.dbid)) + N'.' + quotename(object_name(qp.objectid, qp.dbid)) as obj_name&lt;/p&gt;
&lt;p&gt;, qp.query_plan.value( N'(/sql:ShowPlanXML/sql:BatchSequence/sql:Batch/sql:Statements/sql:StmtSimple[@StatementType = &amp;quot;SELECT&amp;quot;])[1]/@StatementText', 'nvarchar(max)' ) as plan_stmt_text&lt;/p&gt;
&lt;p&gt;, qp.query_plan.value( N'(/sql:ShowPlanXML/sql:BatchSequence/sql:Batch/sql:Statements/sql:StmtSimple[@StatementType = &amp;quot;SELECT&amp;quot;]/sql:QueryPlan/sql:RelOp/descendant::*/sql:ScalarOperator[contains(@ScalarString, &amp;quot;CONVERT_IMPLICIT&amp;quot;)])[1]/@ScalarString', 'nvarchar(4000)' ) as scalar_string&lt;/p&gt;
&lt;p&gt;from sys.dm_exec_query_stats as qs&lt;/p&gt;
&lt;p&gt;cross apply sys.dm_exec_query_plan(qs.plan_handle) as qp&lt;/p&gt;
&lt;p&gt;where qp.query_plan.exist( N'/sql:ShowPlanXML/sql:BatchSequence/sql:Batch/sql:Statements/sql:StmtSimple[@StatementType = &amp;quot;SELECT&amp;quot;]/sql:QueryPlan/sql:RelOp/descendant::*/sql:ScalarOperator[contains(@ScalarString, &amp;quot;CONVERT_IMPLICIT&amp;quot;)]' ) = 1;&lt;/p&gt;
&lt;p&gt; You can also use the new DMV sys.dm_exec_text_query_plan and do a text-based search which is less accurate.&lt;/p&gt;
&lt;p&gt;select qs.sql_handle, qs.plan_handle, qs.total_elapsed_time, qs.last_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time&lt;/p&gt;
&lt;p&gt;, db_name(tqp.dbid) as database_name&lt;/p&gt;
&lt;p&gt;, quotename(object_schema_name(tqp.objectid, tqp.dbid)) + N'.' + quotename(object_name(tqp.objectid, tqp.dbid)) as obj_name&lt;/p&gt;
&lt;p&gt;, tqp.query_plan&lt;/p&gt;
&lt;p&gt;from sys.dm_exec_query_stats as qs&lt;/p&gt;
&lt;p&gt;cross apply sys.dm_exec_text_query_plan( qs.plan_handle, qs.statement_start_offset, qs.statement_end_offset ) as tqp&lt;/p&gt;
&lt;p&gt;where tqp.query_plan like '%&amp;quot;%CONVERT_IMPLICIT%&amp;quot;%';&lt;/p&gt;
&lt;p&gt; Note that you need to change the query (OBJECT_NAME and OBJECT_SCHEMA_NAME usage) if you are not running on SQL Server 2005 SP2. I will be discussing this particular question in a new BLOG article to explain further.&lt;/p&gt;
&lt;p&gt;--&lt;/p&gt;
&lt;p&gt;Umachandar&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1991533" width="1" height="1"&gt;</description></item><item><title>re: Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#1985858</link><pubDate>Thu, 29 Mar 2007 11:16:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1985858</guid><dc:creator>ALZDBA</dc:creator><description>&lt;p&gt;I'm not into this xquery stuff, but can someone please give me a query to select only the queryplans that have implicit conversions ?&lt;/p&gt;
&lt;p&gt;&amp;lt;ScalarOperator ScalarString=&amp;quot;CONVERT_IMPLICIT (whateverconversionwasneeded)&amp;quot;&amp;gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1985858" width="1" height="1"&gt;</description></item><item><title>re: Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#1485766</link><pubDate>Thu, 18 Jan 2007 02:58:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1485766</guid><dc:creator>sangeethashekar</dc:creator><description>&lt;p&gt;Please check out &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/sqlprogrammability/"&gt;http://blogs.msdn.com/sqlprogrammability/&lt;/a&gt; for &amp;nbsp;a series of articles on the Plan Cache and improvements made to the plan cache in SQL Server 2005 RTM. The articles also have a number of examples and queries on plan cache related DMVs that you may find useful.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1485766" width="1" height="1"&gt;</description></item><item><title>re: Performance issue with the DMVs in query</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#611173</link><pubDate>Wed, 31 May 2006 01:53:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:611173</guid><dc:creator>SQL Server Engine Team</dc:creator><description>I just wanted to update that we found and fixed a performance problem with the sys.dm_exec_query_plan DMV. This DMV can take a long time to generate plans for certain statements and this will reduce the overall performance of the query. This bug fix is in SQL Server 2005 SP1 so I would encourage you to try &amp;amp; use the DMV to see if you get noticeable improvements.&lt;br&gt;&lt;br&gt;--&lt;br&gt;Umachandar&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=611173" width="1" height="1"&gt;</description></item><item><title>re: Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#573648</link><pubDate>Tue, 11 Apr 2006 23:24:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:573648</guid><dc:creator>Jerry</dc:creator><description>This stuff is all well and good, but you sure do have to be careful. &amp;nbsp;I attempted to run the final query above on our development box - it still had not completed after 3 minutes. It pegged CPU at 100% for the duration and had taken 16 million page reads by the time I cancelled it. &amp;nbsp;Can't even imagine what it would do to our production box that has 3k trans/sec.&lt;br&gt;&lt;br&gt;Note - I am not trying to be overly critical, I *love* 2005 and the dmv's, and judicious use of the query stats has helped us tremendously. &amp;nbsp;But often things are quite different out here in the wild; I just wanted to caution anyone from running the code on a production box w/o testing it first.&lt;br&gt;&lt;br&gt;Jerry&lt;br&gt;jfos@plex.com &lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=573648" width="1" height="1"&gt;</description></item><item><title>Thank you</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#537572</link><pubDate>Thu, 23 Feb 2006 07:20:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:537572</guid><dc:creator>Great blog</dc:creator><description>Your site is realy very interesting.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=537572" width="1" height="1"&gt;</description></item><item><title>re: Find Top N costly query plans in adhoc batches or modules...</title><link>http://blogs.msdn.com/b/sqltips/archive/2005/10/05/top-n-costly-query-plans.aspx#481599</link><pubDate>Sun, 16 Oct 2005 20:55:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481599</guid><dc:creator>SQL Server Engine Team</dc:creator><description>Sorry, I didn't include the CLR time counters in my query before. That is why you were seeing zero for the elapsed time. See the topic on sys.dm_exec_query_stats DMV. I modified the query to include the CLR counters too.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=481599" width="1" height="1"&gt;</description></item></channel></rss>