Mihai is pointing you to the current system object, dm_exec_cached_plans
. (When I looked up an answer to your question, I found that [syscacheobjects]
is actually deprecated.) Therefore, I did upvote his answer.
However, I still think it could save you time, to check for a single quote after the WHERE
. Using dm_exec_cached_plans
, here's a similar query, using a WHERE
like I gave you before:
SELECT usecounts, cacheobjtype, objtype, text
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE [text] LIKE '%WHERE%''%'
----AND [text] NOT LIKE '%sp_executesql%' ----queries probably ok, with sp_executesql
----WHERE usecounts > 1 ----un-commenting this might also be interesting to check out.
ORDER BY usecounts DESC;
Or, just adding it to Mihai's query:
SELECT
CP.usecounts
, CP.cacheobjtype
, ST.text as sql_Statement
, QP.query_plan
FROM
sys.dm_exec_cached_plans CP
CROSS APPLY sys.dm_exec_sql_text(CP.plan_handle) ST
CROSS APPLY sys.dm_exec_query_plan(CP.plan_handle) QP
WHERE
CP.objtype = 'Adhoc'
AND ST.dbid = DB_ID()
AND ST.text LIKE '%WHERE%''%'
ORDER BY
CP.usecounts DESC
Note the ORDER BY
, built on the thought that the commonly occurring queries are causing frequent exposure. But as you probably already realize... if the suspect code is building the query strings itself, then unique values (like row numbers or customer ids or order numbers, etc.) could produce unique queries that, (if they were built without using sp_executesql
), could actually be the most important to look at, even though they have usecounts = 1
.
As far as the answer to your question goes, ("When do SQL queries get deleted from this table?"): once there are enough queries in the cache to cause "memory pressure," old queries drop out of the cache as new queries come in... and all of it is cleared when SQL restarts. (See http://www.sqlservercentral.com/Forums/Topic1375781-391-1.aspx and http://technet.microsoft.com/en-us/library/ms181055%28v=sql.105%29.aspx)
Hope that helps...