What's the differences between sys.dm_exec_query_stats and sys.dm_exec_cached_plans?












1















As per my understandings, from sys.dm_exec_query_stats, we can cross apply other DMFs to get the statement level cached plans and the query text. From sys.dm_exec_cached_plans, we can get the cached plans and the query text (by cross apply other DMFs). So what's the differences?










share|improve this question
















bumped to the homepage by Community 19 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    1















    As per my understandings, from sys.dm_exec_query_stats, we can cross apply other DMFs to get the statement level cached plans and the query text. From sys.dm_exec_cached_plans, we can get the cached plans and the query text (by cross apply other DMFs). So what's the differences?










    share|improve this question
















    bumped to the homepage by Community 19 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      1












      1








      1


      1






      As per my understandings, from sys.dm_exec_query_stats, we can cross apply other DMFs to get the statement level cached plans and the query text. From sys.dm_exec_cached_plans, we can get the cached plans and the query text (by cross apply other DMFs). So what's the differences?










      share|improve this question
















      As per my understandings, from sys.dm_exec_query_stats, we can cross apply other DMFs to get the statement level cached plans and the query text. From sys.dm_exec_cached_plans, we can get the cached plans and the query text (by cross apply other DMFs). So what's the differences?







      sql-server execution-plan dmv metadata






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 2 '16 at 3:12









      Wafie Ali

      1154




      1154










      asked Dec 1 '16 at 22:05









      Ogrish ManOgrish Man

      1,06942234




      1,06942234





      bumped to the homepage by Community 19 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 19 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          In terms of getting the plan, using sys.dm_exec_query_stats you can use statement_start_offset and End offset to get the plan for individual statement in a stored procedure.



          Eg



          select top 100 object_name(object_id),ps.execution_count as proc_exec_count, qs.plan_generation_num, qs.execution_count as Stmt_exec_count,qs.total_elapsed_time/qs.execution_count as AVG_Run_Time,   qs.total_worker_time/qs.execution_count AS Avg_CPU_Time,
          qs.last_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time,qs.min_rows,qs.max_rows,
          SUBSTRING ([st].[text],
          ([qs].[statement_start_offset] / 2) + 1,
          ((CASE [statement_end_offset]
          WHEN -1 THEN DATALENGTH ([st].[text])
          ELSE [qs].[statement_end_offset] END
          - [qs].[statement_start_offset]) / 2) + 1)
          AS [StatementText],cast(s3.query_plan as xml) query_plan
          from sys.dm_exec_procedure_stats ps
          JOIN [sys].[dm_exec_query_stats] AS [qs]
          ON [ps].[plan_handle] = [qs].[plan_handle]
          cross apply sys.dm_exec_text_query_plan ([qs].[plan_handle], statement_start_offset, statement_end_offset) s3
          CROSS APPLY [sys].[dm_exec_sql_text] ([qs].[sql_handle]) AS [st]
          where database_id=db_id('DB') --and qs.execution_count >100
          and object_id in (object_id('SP Name'))
          ORDER BY avg_run_time DESC


          And obviously it has different information regarding query execution stats and plans.






          share|improve this answer

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "182"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f156951%2fwhats-the-differences-between-sys-dm-exec-query-stats-and-sys-dm-exec-cached-pl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In terms of getting the plan, using sys.dm_exec_query_stats you can use statement_start_offset and End offset to get the plan for individual statement in a stored procedure.



            Eg



            select top 100 object_name(object_id),ps.execution_count as proc_exec_count, qs.plan_generation_num, qs.execution_count as Stmt_exec_count,qs.total_elapsed_time/qs.execution_count as AVG_Run_Time,   qs.total_worker_time/qs.execution_count AS Avg_CPU_Time,
            qs.last_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time,qs.min_rows,qs.max_rows,
            SUBSTRING ([st].[text],
            ([qs].[statement_start_offset] / 2) + 1,
            ((CASE [statement_end_offset]
            WHEN -1 THEN DATALENGTH ([st].[text])
            ELSE [qs].[statement_end_offset] END
            - [qs].[statement_start_offset]) / 2) + 1)
            AS [StatementText],cast(s3.query_plan as xml) query_plan
            from sys.dm_exec_procedure_stats ps
            JOIN [sys].[dm_exec_query_stats] AS [qs]
            ON [ps].[plan_handle] = [qs].[plan_handle]
            cross apply sys.dm_exec_text_query_plan ([qs].[plan_handle], statement_start_offset, statement_end_offset) s3
            CROSS APPLY [sys].[dm_exec_sql_text] ([qs].[sql_handle]) AS [st]
            where database_id=db_id('DB') --and qs.execution_count >100
            and object_id in (object_id('SP Name'))
            ORDER BY avg_run_time DESC


            And obviously it has different information regarding query execution stats and plans.






            share|improve this answer






























              0














              In terms of getting the plan, using sys.dm_exec_query_stats you can use statement_start_offset and End offset to get the plan for individual statement in a stored procedure.



              Eg



              select top 100 object_name(object_id),ps.execution_count as proc_exec_count, qs.plan_generation_num, qs.execution_count as Stmt_exec_count,qs.total_elapsed_time/qs.execution_count as AVG_Run_Time,   qs.total_worker_time/qs.execution_count AS Avg_CPU_Time,
              qs.last_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time,qs.min_rows,qs.max_rows,
              SUBSTRING ([st].[text],
              ([qs].[statement_start_offset] / 2) + 1,
              ((CASE [statement_end_offset]
              WHEN -1 THEN DATALENGTH ([st].[text])
              ELSE [qs].[statement_end_offset] END
              - [qs].[statement_start_offset]) / 2) + 1)
              AS [StatementText],cast(s3.query_plan as xml) query_plan
              from sys.dm_exec_procedure_stats ps
              JOIN [sys].[dm_exec_query_stats] AS [qs]
              ON [ps].[plan_handle] = [qs].[plan_handle]
              cross apply sys.dm_exec_text_query_plan ([qs].[plan_handle], statement_start_offset, statement_end_offset) s3
              CROSS APPLY [sys].[dm_exec_sql_text] ([qs].[sql_handle]) AS [st]
              where database_id=db_id('DB') --and qs.execution_count >100
              and object_id in (object_id('SP Name'))
              ORDER BY avg_run_time DESC


              And obviously it has different information regarding query execution stats and plans.






              share|improve this answer




























                0












                0








                0







                In terms of getting the plan, using sys.dm_exec_query_stats you can use statement_start_offset and End offset to get the plan for individual statement in a stored procedure.



                Eg



                select top 100 object_name(object_id),ps.execution_count as proc_exec_count, qs.plan_generation_num, qs.execution_count as Stmt_exec_count,qs.total_elapsed_time/qs.execution_count as AVG_Run_Time,   qs.total_worker_time/qs.execution_count AS Avg_CPU_Time,
                qs.last_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time,qs.min_rows,qs.max_rows,
                SUBSTRING ([st].[text],
                ([qs].[statement_start_offset] / 2) + 1,
                ((CASE [statement_end_offset]
                WHEN -1 THEN DATALENGTH ([st].[text])
                ELSE [qs].[statement_end_offset] END
                - [qs].[statement_start_offset]) / 2) + 1)
                AS [StatementText],cast(s3.query_plan as xml) query_plan
                from sys.dm_exec_procedure_stats ps
                JOIN [sys].[dm_exec_query_stats] AS [qs]
                ON [ps].[plan_handle] = [qs].[plan_handle]
                cross apply sys.dm_exec_text_query_plan ([qs].[plan_handle], statement_start_offset, statement_end_offset) s3
                CROSS APPLY [sys].[dm_exec_sql_text] ([qs].[sql_handle]) AS [st]
                where database_id=db_id('DB') --and qs.execution_count >100
                and object_id in (object_id('SP Name'))
                ORDER BY avg_run_time DESC


                And obviously it has different information regarding query execution stats and plans.






                share|improve this answer















                In terms of getting the plan, using sys.dm_exec_query_stats you can use statement_start_offset and End offset to get the plan for individual statement in a stored procedure.



                Eg



                select top 100 object_name(object_id),ps.execution_count as proc_exec_count, qs.plan_generation_num, qs.execution_count as Stmt_exec_count,qs.total_elapsed_time/qs.execution_count as AVG_Run_Time,   qs.total_worker_time/qs.execution_count AS Avg_CPU_Time,
                qs.last_elapsed_time, qs.min_elapsed_time, qs.max_elapsed_time,qs.min_rows,qs.max_rows,
                SUBSTRING ([st].[text],
                ([qs].[statement_start_offset] / 2) + 1,
                ((CASE [statement_end_offset]
                WHEN -1 THEN DATALENGTH ([st].[text])
                ELSE [qs].[statement_end_offset] END
                - [qs].[statement_start_offset]) / 2) + 1)
                AS [StatementText],cast(s3.query_plan as xml) query_plan
                from sys.dm_exec_procedure_stats ps
                JOIN [sys].[dm_exec_query_stats] AS [qs]
                ON [ps].[plan_handle] = [qs].[plan_handle]
                cross apply sys.dm_exec_text_query_plan ([qs].[plan_handle], statement_start_offset, statement_end_offset) s3
                CROSS APPLY [sys].[dm_exec_sql_text] ([qs].[sql_handle]) AS [st]
                where database_id=db_id('DB') --and qs.execution_count >100
                and object_id in (object_id('SP Name'))
                ORDER BY avg_run_time DESC


                And obviously it has different information regarding query execution stats and plans.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 1 '16 at 23:31

























                answered Dec 1 '16 at 23:21









                jesijesijesijesi

                878419




                878419






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Database Administrators Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f156951%2fwhats-the-differences-between-sys-dm-exec-query-stats-and-sys-dm-exec-cached-pl%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Liste der Baudenkmale in Friedland (Mecklenburg)

                    Single-Malt-Whisky

                    Czorneboh