question

Nick Kavadias avatar image
Nick Kavadias asked

capturing showplan XML in traces/profiler for ONLY CPU greater than 999

I have a database that has a lot of adhoc sql being run against it, so i don't want to capture all execution plans. Only want those plans that are consuming lots of CPU.

Is there an elegant way to do this? I cannot seem to filter out all the showplans for insignificant queries

execution-planprofilertracesql-tracetrace-events
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered

No, there's no way to filter what you capture using trace events. What you can do though is to pull the execution plan out of the cache. You can query the dynamic management object sys.dm_exec_query_stats. That will give you aggregate information on the queries that have been running on the system. You can use this to determine which queries you're interested in, the longest running, most frequently called, etc. That comes with a plan_handle that you can then use to query against the dynamic management object sys.dm_exec_query_plan to get the execution plan for the longest running queries.

If you have a lot of ad hoc queries running, you might want to automate this to run every 1/2 hour or some other interval since these queries are cache dependent.

Also, since you're looking at ad hoc queries, you can use the query_hash in sys.dm_exec_query_stats as a means of combining queries that are similar to get better information. Of course, if you do that, you will need to worry about having X number of query plans returned, so there might be a bit of a balancing act needed there.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.