• SQL Server
  • Log Shipping Tricks Demo
  • SQLCruise Alaska 2012 Pics
SQLSoldier News From the Frontlines

Performing Fast Searches of Query Plans

July 12, 2012 8:54 am / 1 Comment / SQLSoldier

Performing Fast Searches of Query Plans

XML: Fast as a Turtle

XML: Fast as a Turtle


I've made blog posts in the past on using the exist() method to search for query plans with specific attributes (e.g., Find Cached Query Plans By Index Name). Another good aspect of the exist() method is that it is a much faster way to search for a specific string inside of a query plan than trying to roll your own search by converting to text.

I decided to write this post after reading a great post by Grant Fritchey (blog|@ScaryDBA) called Querying Information from the Plan Cache, Simplified. In this post, Grant compares rolling your own search of the XML vs. extensive parsing of the XML vs. doing a text search of text plan from sys.dm_exec_text_query_plan. SQL Server is not an exceptional performer when it comes to extensive XML parsing. Likewise, converting the query plan to text and then searching it using string functions like LIKE is not a great solution either. Grant was able to get really good performance by doing the LIKE search on the text query plan from sys.dm_exec_text_query_plan.

However, you can actually do very quick string searches within XML using the exist() method. Grant was able to get his query that performs extensive XML parsng down from 30 seconds to 8 seconds using sys.dm_exec_text_query_plan. Now to be perfectly clear, Grant's query runs much faster on my laptop than it does on whatever machine he was using. Likely, my cache is much smaller right now due to less activity. His text solution runs in 3 seconds on my laptop. My solution using the exist() method runs in 2 seconds. It's only slightly faster than the text solution.

Is the speed difference enough to make it worth the extra effort of figuring out how to write the exist() clause? Maybe not. But if you do it enough, that part isn't so painful. It was actually very easy to write the exist() clause for my solution. Give Grant's post a read to see his versions of the queries. Here is my solution using the exist() clause:

WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS sqlx) 
SELECT DB_NAME(deqp.dbid),
    SUBSTRING(dest.text, (deqs.statement_start_offset / 2) + 1,
    (CASE deqs.statement_end_offset
        WHEN -1 THEN DATALENGTH(dest.text)
        ELSE deqs.statement_end_offset
    END - deqs.statement_start_offset) / 2 + 1) AS StatementText,
    deqs.statement_end_offset,
    deqs.statement_start_offset,
    deqp.query_plan,
    deqs.execution_count,
    deqs.total_elapsed_time,
    deqs.total_logical_reads,
    deqs.total_logical_writes
FROM    sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE deqp.query_plan.exist('//sqlx:StmtSimple[@StatementOptmEarlyAbortReason="TimeOut"]') = 1;
Posted in: SQL Server / Tagged: T-SQL, Tips & Tricks, XML in SQL

One Thought on “Performing Fast Searches of Query Plans”

  1. Pingback: Something for the Weekend - SQL Server Links 13/07/12

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation

← Previous Post
Next Post →
<

Remote DBA Services
- serious SQL Server expertise for less than a full-time DBA
My Articles
 
My Book
Check out my interview on

Extreme Data Recovery (with Argenis Fernandez)
10 Things all BI System Administrators Should Know
Upcoming Events
    All events shown in Pacific Time

    No events to show

RSS My SQL Server Magazine Articles

  • Database Mirroring for Disaster Recovery September 16, 2011
  • Comparative Review: Database Schema Comparison Tools August 24, 2011
  • 3 Log Shipping Techniques June 22, 2011
  • Hardening SQL Server June 20, 2011
  • Review: ScriptLogic Security Explorer for SQL Server February 8, 2011

Tags

31 Days of Disaster Recovery Architecture Automation CDC & Change Tracking Data Architecture VC Database Mirroring DBCC Denali Disaster Recovery Dynamic Management Views Extended Events Gamers & Geeks General Discussion High Availability How do I ... ? Humor Idera ACE Program Internals MCM Meme Monday Performance & Optimization PowerShell Professional Development Replication Security SQLBits SQL PASS SQL PASS Summit SQLRally SQL Saturday SQL Server Magazine SQL University SSAS & BI SSIS SSMS SSRS T-SQL T-SQL Tuesday tempDB Tips & Tricks Travel Troubleshooting Undocumented Stuff Whitepapers XML in SQL

News

Download my Powershell Scripts

The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them.

Backup a database
Restore a database
Scan a server to find a free port
Query DNS to get the FQDN of a server


To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.


Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine.
August issue of TechNet Magazine's SQL Q&A column

Protect our Heroes

© Copyright 2012 - Robert L Davis
Infinity Theme by DesignCoral / WordPress

Twitter Twitter 
LinkedIn LinkedIn 
TLF TLF RSS RSS 
WritersCafe WritersCafe 
SQLPASS SQLPASS 
Facebook Facebook
grab this