Stored Procuders which has multiple execution plans

  • Hi All,

    I have few doubts on execution plans.

    1) How to find the Stored Procuders which has/Generates multiple execution plans?

    2) How to avoid the generation of multiple execution plans by SP's.

    Please help me on this guys..:-)

    Thanks in advance.

    Regards,

    Vijay Singh

  • by default query processor generates multiple execution plan for any query that you submit and chooses one of them based on least cost. This is by design and you cannot control it. There are some cases where in if query processor determines that choosing amongst various execution plan is going to take more time, it generates one plan and executes the query as per that plan.

    you can click on include actual execution plan to see what is the plan selected by the query processor.



    Pradeep Singh

  • The only way you'll have multiple plans per procedure is if the procedure has been run with different SET options. Certain set options (see BoL for details) change the way the query must be executed and hence get a different plan.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks for the updates..:-)

    When i saw the Cache i got SPs with multiple cache plan even after not changing the SET options.

    Is there any other way to avoid this?

    Thanks,

    Vijay Singh

  • what query are you running to see that?



    Pradeep Singh

  • Hi Pradeep,

    Below is the query :

    SELECT db_name(st.dbid) DBName,

    object_schema_name(st.objectid, dbid) SchemaName,

    object_name(st.objectid, dbid) StoredProcedure,

    MAX(cp.usecounts) Execution_count,

    st.text [Plan_Text]

    FROM sys.dm_exec_cached_plans cp

    CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st

    WHERE db_name(st.dbid) IS NOT NULL

    AND cp.objtype = 'proc'

    GROUP BY cp.plan_handle,

    db_name(st.dbid),

    object_schema_name(objectid, st.dbid),

    object_name(objectid, st.dbid),

    st.text

    ORDER BY MAX(cp.usecounts) DESC

    Thanks,

    Vijay Singh

  • In some cases, query processor may keep two plans, 1) without using parallelism, 2) using parallism and use either of them.

    beyond that, i cant think why you are seeing several plans for one sp.



    Pradeep Singh

  • ps. (11/16/2010)


    In some cases, query processor may keep two plans, 1) without using parallelism, 2) using parallism and use either of them.

    Nope. Recently debunked by Paul White (I'll leave it to him to come up with url, I'm working on a freshly installed machine)

    If there are multiple plans (ie with different plan handles) then either set options are different or (user is different and query is not safe for reuse between different users)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Yes i have that URL. He explained that a parallel plan can also be executed by a single processor.

    http://sqlblog.com/blogs/paul_white/default.aspx%5B/url%5D



    Pradeep Singh

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply