Question

Setup:

Using SQL Server 2008 R2.

We've got a stored procedure that has been intermittently running very long. I'd like to test a theory that parameter sniffing is causing the query engine to choose a bad plan.

Question:

How can I copy the query's execution plans from one database to another (test) database?

Note:

I'm fully aware that this may not be parameter sniffing issues. However, I'd like to go through the motions of creating a test plan and using it, if at all possible. Therefore please do not ask me to post code and/or table schema, since this is irrelevant at this time.

Was it helpful?

Solution

Plans are not portable, they bind to object IDs. You can use planguides, but they are strictly tied to the database. What you have to do is test on a restored backup of the same database. On a restored backup you can use a planguide. But for relevance the physical characteristics of the machines should be similar (CPUs, RAM, Disks).

Normally though one does not need to resort to such shenanigans as copy the plans. Looking at actual execution plans all the answers are right there.

OTHER TIPS

Have you tried using OPTIMIZE FOR clause? With it you can tune your procedure easier, and without the risk that plan that you copy from another database will be inappropriate due to differences in those databases (if copying the plan is even possible).

http://www.mssqltips.com/sqlservertip/1354/optimize-parameter-driven-queries-with-sql-server-optimize-for-hint/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top