Skip to content

Instantly share code, notes, and snippets.

@peschkaj
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peschkaj/101e6656da840447ad85 to your computer and use it in GitHub Desktop.
Save peschkaj/101e6656da840447ad85 to your computer and use it in GitHub Desktop.
Find the cost and memory grant of queries in the plan cache
SELECT qs.query_plan_hash ,
query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryHash, 3)) = xs:hexBinary(sql:column("qs.query_hash"))]/@StatementSubTreeCost)',
'float') AS cost ,
query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryHash, 3)) = xs:hexBinary(sql:column("qs.query_hash"))]/p:QueryPlan/p:MemoryGrantInfo/@SerialDesiredMemory)', 'float') AS serial_desired_memory,
query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004
/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryHash, 3)) = xs:hexBinary(sql:column("qs.query_hash"))]/p:QueryPlan/p:MemoryGrantInfo/@SerialRequiredMemory)', 'float') AS serial_required_memory,
*
FROM sys.dm_exec_query_stats qs
OUTER APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY 3 DESC
@peschkaj
Copy link
Author

Updated the formatting and stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment