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

Hello,

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

SELECT TOP 20
    qs.sql_handle,
    qs.execution_count,
    total_CPU_inSeconds = --Converted from microseconds
        qs.total_worker_time/1000000,
    average_CPU_inSeconds = --Converted from microseconds
        (qs.total_worker_time/1000000) / qs.execution_count,
    total_elapsed_time_inSeconds = --Converted from microseconds
        qs.total_elapsed_time/1000000,
   st.text,
   qp.query_plan
from
    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:

CREATE PROCEDURE [dbo]…

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!
adrian78Asked:
Who is Participating?
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

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

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%?

Thanks!
0
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?
0
adrian78Author Commented:
Thank you!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.