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

Find Cached Query Plans By Index Name

December 14, 2010 3:36 pm / 5 Comments / SQLSoldier

Find Cached Query Plans By Index Name

A following question came in via the #sqlhelp hash tag on Twitter:

Trying to find the plans using a specific index in the plan cache using XQuery – any ideas out there? #sqlhelp

I replied with a little information but felt the full answer could not be shared 140 characters at a time. So here is the full query that I wrote. I am using sql:variable() to pass the index name into the exist() method. I’m using a relative XML path to locate any Object reference stating that it is an index with the name defined above.

The Query

DECLARE @Index SYSNAME

SET @Index = '[PK_IndexName]';

WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS sqlx) 
SELECT object_name(st.objectid, st.dbid) AS ObjectName,
    qp.query_plan AS QueryPlan,
    st.text AS ObjectText
FROM sys.dm_exec_cached_plans AS cp 
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) AS qp 
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) AS st 
WHERE qp.dbid = DB_ID()
AND qp.query_plan.exist('//sqlx:Object[@Index=sql:variable("@Index")]') = 1;
Posted in: SQL Server / Tagged: Dynamic Management Views, T-SQL, Tips & Tricks, XML in SQL

5 Thoughts on “Find Cached Query Plans By Index Name”

  1. WIDBA on December 15, 2010 at 6:39 am said:

    Just wanted to point out that in addition to getting a solution, I learned something from this relatively simple script – Thanks Again.

    Reply↓
  2. SQLSoldier on December 15, 2010 at 8:18 pm said:

    You’re welcome!!

    Reply↓
  3. Pingback: Performing Fast Searches of Query Plans | SQLSoldier

  4. Andre Ranieri on February 4, 2013 at 1:28 pm said:

    Robert – Thanks for posting this. It seems that very often when I Google on some SQL challenge, I find the results in your blog.

    Thanks,

    Andre Ranieri

    Reply↓
    • SQLSoldier on February 5, 2013 at 11:00 am said:

      Thanks Andre! Glad you find my blog useful.

      Reply↓

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