Find statements in the plan cache
find_cached_queriesLists SQL Server plan-cache statements with execution counts, durations, CPU, reads, and plan handles to diagnose slow queries when Query Store is off or inspect current plans.
Instructions
Lists this database's statements in SQL Server's plan cache, leaving out the queries this server sends to read the cache and the sessions, with what their runs cost since each plan was cached: per statement, when its plan was compiled and how many times (plan_generation_num), its last run, executions, total, average, maximum and last duration, CPU and logical reads, physical reads, rows, degree of parallelism, memory grant and spills, the query and plan hashes, the first 300 characters of the statement, and the plan_handle with the statement's offsets, which get_query_plan_summary takes. With object_name, the statements of that procedure, function or trigger, the variants SQL Server 2022+ keeps of a parameter-sensitive one included, and the totals SQL Server keeps for a procedure, scalar function or trigger. Use it when a query is slow and the Query Store is off, as it is by default before SQL Server 2022, or to see the plan in use now. The cache keeps no history: a plan evicted, recompiled or lost to a restart takes its figures with it, so where the Query Store is on use find_query_store_queries and get_query_store_history for what changed over days. Needs VIEW SERVER STATE (2022+: VIEW SERVER PERFORMANCE STATE).
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"database": "<database>"
},
"name": "find_cached_queries"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| top | No | Most statements to return (default 20, at most 100). | |
| schema | No | Schema of object_name, which it needs. Empty searches every schema. Example: 'AutoRegister' | |
| database | Yes | Name of the database as registered in the configuration. | |
| order_by | No | 'duration' (default): the most total time since the plan was cached; 'cpu', 'logical_reads' or 'physical_reads': the most of that in total; 'executions': the most run; 'avg_duration': the slowest execution on average; 'recompiles': the highest plan_generation_num; 'newest': the plans compiled most recently. | duration |
| object_name | No | Procedure, function or trigger whose statements to list, bare, with no schema prefix; the answer then adds, for a procedure, scalar function or trigger, its own totals in module_stats. Empty means any statement, inside a module or not. Example: 'AutoRegisterEquity' | |
| text_pattern | No | Part of the statement's text, matched anywhere in it as written: brackets, % and _ are plain characters, so a fragment copied from a statement_text finds it. At most 1000 characters. Empty means any text. Example: '[dbo].[GenericSecurity]' |