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 from 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
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.