SQL Server - Query Optimization - Understanding Query Plan (Stored Procedure Getting Created Again and again?)


I ran the following query which I found online which shows the most consuming queries:

    total_CPU_inSeconds = --Converted from microseconds
    average_CPU_inSeconds = --Converted from microseconds
        (qs.total_worker_time/1000000) / qs.execution_count,
    total_elapsed_time_inSeconds = --Converted from microseconds
    sys.dm_exec_query_stats as qs
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as st
    cross apply sys.dm_exec_query_plan (qs.plan_handle) as qp
ORDER BY qs.total_worker_time desc

The results of this are a bit confusing for me.  The 20 results that get returned all have a "text" field which contains 2 different queries only… all of them start like this:


So my question, why is my stored procedure getting re-created so much (execution count of 30,000)?  I thought you just create a stored procedure once and it doesn't need to be re-created.  Perhaps I'm mis-understanding something…

Thanks in advance!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
I would be curious what the object name is, as in this query:

SELECT top 10 OBJECT_NAME(objectid), * 
   FROM sys.dm_exec_query_stats 
   CROSS APPLY sys.dm_exec_sql_text(sql_handle)
   ORDER BY max_logical_reads DESC

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
So my question, why is my stored procedure getting re-created so much (execution count of 30,000)?  
Isn't recreated but called. The text is only showing the stored procedure definition but doesn't means that is recreating it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adrian78Author Commented:
Hi Guys,

Thanks for the reply - I apologize for the delay.

Bob: I ran your query and the top object was "NULL".  The second most popular was one of my stored procedures and the third most popular was another stored procedure.  Then after that there were more NULLs and more of the same procedures previously mentioned.

Vitor: I guess that answers my question.  So to confirm, it's simply showing the procedure definition but that doesn't mean it's creating it… it's just calling it.  Do we know this 100%?

Vitor MontalvãoMSSQL Senior EngineerCommented:
Well, if it's only creating it should return errors saying that stored procedure already exists, right?
Unless if you are dropping it first but then where's the DROP PROCEDURE command?
adrian78Author Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.