Executing a Client Query Exactly the same in SSMS
I have a parameter sniffing issue in sql server 2005 for a dynamic, parameterized query that is generated in a C# client via ado.net and accessed in a web application from IIS using LocalSystem as the application pool's identity.
When I run the query in SSMS it is very fast, but it times out after 30 seconds when run f开发者_如何转开发rom the client, so I am certain of parameter sniffing.
Now, I want to look at the query plan for the query that is cached from the client, but I can't seem to replicate the query in SSMS to use that cached plan.
I ran a trace and have the exact query, and all of the set parameters the client used, but this still does not give me the cached query plan when I run it in SSMS. I assume this has something to do with either the ApplicationName, NTUserName, or LoginName (also provided by the trace), but I am not sure which of these or which combination of these I need to set (or how to do it in SSMS) to run the query so that it uses the cached query plan.
get the query to run, then while it is running execute the follow script
SELECT session_id, blocking_session_id, command, cpu_time, reads, writes, logical_reads, row_count, total_elapsed_time, granted_query_memory, DB_NAME(DATABASE_ID),
last_wait_type, wait_resource, start_time, open_transaction_count, query_plan, text
FROM master.sys.dm_exec_requests (nolock) CROSS APPLY sys.dm_exec_query_plan(plan_handle) CROSS APPLY sys.dm_exec_sql_text(sql_handle)
WHERE session_id <> @@SPID
this will return the estimated plan as well as other info of all currently running queries (you need VIEW SERVER STATE permission).
Usually UPDATE STATISTICS would help you in this situations, or making your own statistics. If you'd like help with the query plan host the XML somewhere and i'll be glad to help.
精彩评论