SQL Server Query Store - What is considered an 'ad-hoc' query?












8















I've been doing a deep dive into the SQL Server Query Store and I often see references to 'ad-hoc' queries. However, I haven't seen what the Query Store determines an ad-hoc query to be. I've seen places where it could be inferred to be a query without parameters or a query executed just one time. Does a formal definition exist for this? I don't mean in general. I mean as it relates to the Query Store.



As an example, this page shows an example of removing ad-hoc queries from the query store, but it appears the criteria it's using is an execution count of just one. This seems to be an odd definition of an ad-hoc query. BTW, if you go to the page, search for 'Delete ad-hoc queries'.










share|improve this question

























  • See this question. stackoverflow.com/questions/2460954/what-is-ad-hoc-query

    – SqlWorldWide
    Oct 11 '17 at 13:19











  • @SqlWorldWide - I have reworded my question. I don't mean in general. I mean as it relates to the Query Store.

    – Randy Minder
    Oct 11 '17 at 13:21











  • How is your query store configured - all or auto ?

    – Kin
    Oct 11 '17 at 14:14











  • @Kin - It's set as Auto

    – Randy Minder
    Oct 11 '17 at 14:14
















8















I've been doing a deep dive into the SQL Server Query Store and I often see references to 'ad-hoc' queries. However, I haven't seen what the Query Store determines an ad-hoc query to be. I've seen places where it could be inferred to be a query without parameters or a query executed just one time. Does a formal definition exist for this? I don't mean in general. I mean as it relates to the Query Store.



As an example, this page shows an example of removing ad-hoc queries from the query store, but it appears the criteria it's using is an execution count of just one. This seems to be an odd definition of an ad-hoc query. BTW, if you go to the page, search for 'Delete ad-hoc queries'.










share|improve this question

























  • See this question. stackoverflow.com/questions/2460954/what-is-ad-hoc-query

    – SqlWorldWide
    Oct 11 '17 at 13:19











  • @SqlWorldWide - I have reworded my question. I don't mean in general. I mean as it relates to the Query Store.

    – Randy Minder
    Oct 11 '17 at 13:21











  • How is your query store configured - all or auto ?

    – Kin
    Oct 11 '17 at 14:14











  • @Kin - It's set as Auto

    – Randy Minder
    Oct 11 '17 at 14:14














8












8








8


1






I've been doing a deep dive into the SQL Server Query Store and I often see references to 'ad-hoc' queries. However, I haven't seen what the Query Store determines an ad-hoc query to be. I've seen places where it could be inferred to be a query without parameters or a query executed just one time. Does a formal definition exist for this? I don't mean in general. I mean as it relates to the Query Store.



As an example, this page shows an example of removing ad-hoc queries from the query store, but it appears the criteria it's using is an execution count of just one. This seems to be an odd definition of an ad-hoc query. BTW, if you go to the page, search for 'Delete ad-hoc queries'.










share|improve this question
















I've been doing a deep dive into the SQL Server Query Store and I often see references to 'ad-hoc' queries. However, I haven't seen what the Query Store determines an ad-hoc query to be. I've seen places where it could be inferred to be a query without parameters or a query executed just one time. Does a formal definition exist for this? I don't mean in general. I mean as it relates to the Query Store.



As an example, this page shows an example of removing ad-hoc queries from the query store, but it appears the criteria it's using is an execution count of just one. This seems to be an odd definition of an ad-hoc query. BTW, if you go to the page, search for 'Delete ad-hoc queries'.







sql-server query-store






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 11 '17 at 13:21







Randy Minder

















asked Oct 11 '17 at 13:17









Randy MinderRandy Minder

97011124




97011124













  • See this question. stackoverflow.com/questions/2460954/what-is-ad-hoc-query

    – SqlWorldWide
    Oct 11 '17 at 13:19











  • @SqlWorldWide - I have reworded my question. I don't mean in general. I mean as it relates to the Query Store.

    – Randy Minder
    Oct 11 '17 at 13:21











  • How is your query store configured - all or auto ?

    – Kin
    Oct 11 '17 at 14:14











  • @Kin - It's set as Auto

    – Randy Minder
    Oct 11 '17 at 14:14



















  • See this question. stackoverflow.com/questions/2460954/what-is-ad-hoc-query

    – SqlWorldWide
    Oct 11 '17 at 13:19











  • @SqlWorldWide - I have reworded my question. I don't mean in general. I mean as it relates to the Query Store.

    – Randy Minder
    Oct 11 '17 at 13:21











  • How is your query store configured - all or auto ?

    – Kin
    Oct 11 '17 at 14:14











  • @Kin - It's set as Auto

    – Randy Minder
    Oct 11 '17 at 14:14

















See this question. stackoverflow.com/questions/2460954/what-is-ad-hoc-query

– SqlWorldWide
Oct 11 '17 at 13:19





See this question. stackoverflow.com/questions/2460954/what-is-ad-hoc-query

– SqlWorldWide
Oct 11 '17 at 13:19













@SqlWorldWide - I have reworded my question. I don't mean in general. I mean as it relates to the Query Store.

– Randy Minder
Oct 11 '17 at 13:21





@SqlWorldWide - I have reworded my question. I don't mean in general. I mean as it relates to the Query Store.

– Randy Minder
Oct 11 '17 at 13:21













How is your query store configured - all or auto ?

– Kin
Oct 11 '17 at 14:14





How is your query store configured - all or auto ?

– Kin
Oct 11 '17 at 14:14













@Kin - It's set as Auto

– Randy Minder
Oct 11 '17 at 14:14





@Kin - It's set as Auto

– Randy Minder
Oct 11 '17 at 14:14










4 Answers
4






active

oldest

votes


















7














After a little bit of searching, I wasn't able to find a concrete and satisfying source from Microsoft's documentation to answer this question. There are many good third-party descriptions and definitions of adhoc/ad-hoc/ad hoc out there, but for the specificity of this question I think one close to the source is ideal.



Moving past generic (but still accurate) definitions like this SO post (thanks SqlWorldWide), if we look at what the documentation does say on the topic, it agrees with what you've mentioned about the definition being based off of the number of executions, I think to the point that we can take it as fact.



The article on optimizing for ad hoc workflows says,




When this option is set to 1, the Database Engine stores a small
compiled plan stub in the plan cache when a batch is compiled for the
first time, instead of the full compiled plan. This helps to relieve
memory pressure by not allowing the plan cache to become filled with
compiled plans that are not reused. The compiled plan stub allows the
Database Engine to recognize that this ad hoc batch has been compiled
before but has only stored a compiled plan stub, so when this batch is
invoked (compiled or executed) again, the Database Engine compiles the
batch ... and adds
the full compiled plan to the plan cache.




So it would appear the the ad hoc query server configuration option also uses the definition of a single execution as the definition of ad hoc. If the query continues to get executed and generates the same plan it would no longer be treated as such.



The article on Best Practices for Query Store also aligns with this,




Compare the number of distinct query_hash values with the total number
of entries in sys.query_store_query. If the ratio is close to 1 your
ad-hoc workload generates different queries.




This, of course, is for queries that are not already used as stored procedures, parameterized, etc. because they can be recognized and treated appropriately right off the bat.



So based on all this we can say that a query is treated as ad hoc if:




  • It is not parameterized

  • It is not programmatically stored in the database (stored proc, function, trigger, etc.)

  • The same query is only executed one time
    OR The same query is executed multiple times but generates a different query plan for each subsequent execution.






share|improve this answer

































    4














    For ad-hoc queries, the object_id column in the sys.query_store_query DMV will be 0, as stated in the sys.query_store_query documentation:




    object_id:



    ID of the database object that the query is part of (stored procedure,
    trigger, CLR UDF/UDAgg, etc.). 0 if the query is not executed as part
    of a database object (ad-hoc query).




    You can identify ad-hoc queries based on that value, even though it is not explicitly stated as "this is the definition of ad-hoc queries". :)






    share|improve this answer































      0














      The term ad-hoc is used to refer to queries that have only been executed once. This is consistent with the definition used for the database setting "Optimize for ad-hoc workloads".



      The link you reference regards deleting ad-hoc queries includes this specific definition "Delete ad-hoc queries: This deletes the queries that were only executed only once and that are more than 24 hours old."






      share|improve this answer































        0














        Ad-hoc from sys.dm_exec_cached_plans dmv's objtype column in docs.microsoft.com.




        Adhoc: Ad hoc query. Refers to Transact-SQL submitted as language events by using osql or sqlcmd instead of as remote procedure calls.




        One use of this column is to see in case of Queries with multiple plans to see if the multiple plans are caused by Adhoc.



            SELECT  DISTINCT 
        QCP.objtype
        ,MultipleQ.PlanCount
        ,qStat.query_hash
        ,sText.text AS QueryText
        FROM (
        SELECT query_hash,
        COUNT(query_hash) AS PlanCount
        FROM sys.dm_exec_query_stats
        GROUP BY query_hash
        ) AS MultipleQ
        INNER JOIN sys.dm_exec_query_stats qStat ON MultipleQ.query_hash = qStat.query_hash
        INNER JOIN sys.dm_exec_cached_plans QCP
        ON QCP.plan_handle = qStat.plan_handle
        CROSS APPLY sys.dm_exec_sql_text(qStat.sql_handle) AS sText
        CROSS APPLY sys.dm_exec_query_plan(qStat.plan_handle) AS qp
        WHERE PlanCount > 1
        ORDER BY MultipleQ.PlanCount DESC





        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%2f188203%2fsql-server-query-store-what-is-considered-an-ad-hoc-query%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          After a little bit of searching, I wasn't able to find a concrete and satisfying source from Microsoft's documentation to answer this question. There are many good third-party descriptions and definitions of adhoc/ad-hoc/ad hoc out there, but for the specificity of this question I think one close to the source is ideal.



          Moving past generic (but still accurate) definitions like this SO post (thanks SqlWorldWide), if we look at what the documentation does say on the topic, it agrees with what you've mentioned about the definition being based off of the number of executions, I think to the point that we can take it as fact.



          The article on optimizing for ad hoc workflows says,




          When this option is set to 1, the Database Engine stores a small
          compiled plan stub in the plan cache when a batch is compiled for the
          first time, instead of the full compiled plan. This helps to relieve
          memory pressure by not allowing the plan cache to become filled with
          compiled plans that are not reused. The compiled plan stub allows the
          Database Engine to recognize that this ad hoc batch has been compiled
          before but has only stored a compiled plan stub, so when this batch is
          invoked (compiled or executed) again, the Database Engine compiles the
          batch ... and adds
          the full compiled plan to the plan cache.




          So it would appear the the ad hoc query server configuration option also uses the definition of a single execution as the definition of ad hoc. If the query continues to get executed and generates the same plan it would no longer be treated as such.



          The article on Best Practices for Query Store also aligns with this,




          Compare the number of distinct query_hash values with the total number
          of entries in sys.query_store_query. If the ratio is close to 1 your
          ad-hoc workload generates different queries.




          This, of course, is for queries that are not already used as stored procedures, parameterized, etc. because they can be recognized and treated appropriately right off the bat.



          So based on all this we can say that a query is treated as ad hoc if:




          • It is not parameterized

          • It is not programmatically stored in the database (stored proc, function, trigger, etc.)

          • The same query is only executed one time
            OR The same query is executed multiple times but generates a different query plan for each subsequent execution.






          share|improve this answer






























            7














            After a little bit of searching, I wasn't able to find a concrete and satisfying source from Microsoft's documentation to answer this question. There are many good third-party descriptions and definitions of adhoc/ad-hoc/ad hoc out there, but for the specificity of this question I think one close to the source is ideal.



            Moving past generic (but still accurate) definitions like this SO post (thanks SqlWorldWide), if we look at what the documentation does say on the topic, it agrees with what you've mentioned about the definition being based off of the number of executions, I think to the point that we can take it as fact.



            The article on optimizing for ad hoc workflows says,




            When this option is set to 1, the Database Engine stores a small
            compiled plan stub in the plan cache when a batch is compiled for the
            first time, instead of the full compiled plan. This helps to relieve
            memory pressure by not allowing the plan cache to become filled with
            compiled plans that are not reused. The compiled plan stub allows the
            Database Engine to recognize that this ad hoc batch has been compiled
            before but has only stored a compiled plan stub, so when this batch is
            invoked (compiled or executed) again, the Database Engine compiles the
            batch ... and adds
            the full compiled plan to the plan cache.




            So it would appear the the ad hoc query server configuration option also uses the definition of a single execution as the definition of ad hoc. If the query continues to get executed and generates the same plan it would no longer be treated as such.



            The article on Best Practices for Query Store also aligns with this,




            Compare the number of distinct query_hash values with the total number
            of entries in sys.query_store_query. If the ratio is close to 1 your
            ad-hoc workload generates different queries.




            This, of course, is for queries that are not already used as stored procedures, parameterized, etc. because they can be recognized and treated appropriately right off the bat.



            So based on all this we can say that a query is treated as ad hoc if:




            • It is not parameterized

            • It is not programmatically stored in the database (stored proc, function, trigger, etc.)

            • The same query is only executed one time
              OR The same query is executed multiple times but generates a different query plan for each subsequent execution.






            share|improve this answer




























              7












              7








              7







              After a little bit of searching, I wasn't able to find a concrete and satisfying source from Microsoft's documentation to answer this question. There are many good third-party descriptions and definitions of adhoc/ad-hoc/ad hoc out there, but for the specificity of this question I think one close to the source is ideal.



              Moving past generic (but still accurate) definitions like this SO post (thanks SqlWorldWide), if we look at what the documentation does say on the topic, it agrees with what you've mentioned about the definition being based off of the number of executions, I think to the point that we can take it as fact.



              The article on optimizing for ad hoc workflows says,




              When this option is set to 1, the Database Engine stores a small
              compiled plan stub in the plan cache when a batch is compiled for the
              first time, instead of the full compiled plan. This helps to relieve
              memory pressure by not allowing the plan cache to become filled with
              compiled plans that are not reused. The compiled plan stub allows the
              Database Engine to recognize that this ad hoc batch has been compiled
              before but has only stored a compiled plan stub, so when this batch is
              invoked (compiled or executed) again, the Database Engine compiles the
              batch ... and adds
              the full compiled plan to the plan cache.




              So it would appear the the ad hoc query server configuration option also uses the definition of a single execution as the definition of ad hoc. If the query continues to get executed and generates the same plan it would no longer be treated as such.



              The article on Best Practices for Query Store also aligns with this,




              Compare the number of distinct query_hash values with the total number
              of entries in sys.query_store_query. If the ratio is close to 1 your
              ad-hoc workload generates different queries.




              This, of course, is for queries that are not already used as stored procedures, parameterized, etc. because they can be recognized and treated appropriately right off the bat.



              So based on all this we can say that a query is treated as ad hoc if:




              • It is not parameterized

              • It is not programmatically stored in the database (stored proc, function, trigger, etc.)

              • The same query is only executed one time
                OR The same query is executed multiple times but generates a different query plan for each subsequent execution.






              share|improve this answer















              After a little bit of searching, I wasn't able to find a concrete and satisfying source from Microsoft's documentation to answer this question. There are many good third-party descriptions and definitions of adhoc/ad-hoc/ad hoc out there, but for the specificity of this question I think one close to the source is ideal.



              Moving past generic (but still accurate) definitions like this SO post (thanks SqlWorldWide), if we look at what the documentation does say on the topic, it agrees with what you've mentioned about the definition being based off of the number of executions, I think to the point that we can take it as fact.



              The article on optimizing for ad hoc workflows says,




              When this option is set to 1, the Database Engine stores a small
              compiled plan stub in the plan cache when a batch is compiled for the
              first time, instead of the full compiled plan. This helps to relieve
              memory pressure by not allowing the plan cache to become filled with
              compiled plans that are not reused. The compiled plan stub allows the
              Database Engine to recognize that this ad hoc batch has been compiled
              before but has only stored a compiled plan stub, so when this batch is
              invoked (compiled or executed) again, the Database Engine compiles the
              batch ... and adds
              the full compiled plan to the plan cache.




              So it would appear the the ad hoc query server configuration option also uses the definition of a single execution as the definition of ad hoc. If the query continues to get executed and generates the same plan it would no longer be treated as such.



              The article on Best Practices for Query Store also aligns with this,




              Compare the number of distinct query_hash values with the total number
              of entries in sys.query_store_query. If the ratio is close to 1 your
              ad-hoc workload generates different queries.




              This, of course, is for queries that are not already used as stored procedures, parameterized, etc. because they can be recognized and treated appropriately right off the bat.



              So based on all this we can say that a query is treated as ad hoc if:




              • It is not parameterized

              • It is not programmatically stored in the database (stored proc, function, trigger, etc.)

              • The same query is only executed one time
                OR The same query is executed multiple times but generates a different query plan for each subsequent execution.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 11 '17 at 14:36

























              answered Oct 11 '17 at 13:44









              LowlyDBALowlyDBA

              6,81152341




              6,81152341

























                  4














                  For ad-hoc queries, the object_id column in the sys.query_store_query DMV will be 0, as stated in the sys.query_store_query documentation:




                  object_id:



                  ID of the database object that the query is part of (stored procedure,
                  trigger, CLR UDF/UDAgg, etc.). 0 if the query is not executed as part
                  of a database object (ad-hoc query).




                  You can identify ad-hoc queries based on that value, even though it is not explicitly stated as "this is the definition of ad-hoc queries". :)






                  share|improve this answer




























                    4














                    For ad-hoc queries, the object_id column in the sys.query_store_query DMV will be 0, as stated in the sys.query_store_query documentation:




                    object_id:



                    ID of the database object that the query is part of (stored procedure,
                    trigger, CLR UDF/UDAgg, etc.). 0 if the query is not executed as part
                    of a database object (ad-hoc query).




                    You can identify ad-hoc queries based on that value, even though it is not explicitly stated as "this is the definition of ad-hoc queries". :)






                    share|improve this answer


























                      4












                      4








                      4







                      For ad-hoc queries, the object_id column in the sys.query_store_query DMV will be 0, as stated in the sys.query_store_query documentation:




                      object_id:



                      ID of the database object that the query is part of (stored procedure,
                      trigger, CLR UDF/UDAgg, etc.). 0 if the query is not executed as part
                      of a database object (ad-hoc query).




                      You can identify ad-hoc queries based on that value, even though it is not explicitly stated as "this is the definition of ad-hoc queries". :)






                      share|improve this answer













                      For ad-hoc queries, the object_id column in the sys.query_store_query DMV will be 0, as stated in the sys.query_store_query documentation:




                      object_id:



                      ID of the database object that the query is part of (stored procedure,
                      trigger, CLR UDF/UDAgg, etc.). 0 if the query is not executed as part
                      of a database object (ad-hoc query).




                      You can identify ad-hoc queries based on that value, even though it is not explicitly stated as "this is the definition of ad-hoc queries". :)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 11 '17 at 13:38









                      Rigerta DemiriRigerta Demiri

                      26216




                      26216























                          0














                          The term ad-hoc is used to refer to queries that have only been executed once. This is consistent with the definition used for the database setting "Optimize for ad-hoc workloads".



                          The link you reference regards deleting ad-hoc queries includes this specific definition "Delete ad-hoc queries: This deletes the queries that were only executed only once and that are more than 24 hours old."






                          share|improve this answer




























                            0














                            The term ad-hoc is used to refer to queries that have only been executed once. This is consistent with the definition used for the database setting "Optimize for ad-hoc workloads".



                            The link you reference regards deleting ad-hoc queries includes this specific definition "Delete ad-hoc queries: This deletes the queries that were only executed only once and that are more than 24 hours old."






                            share|improve this answer


























                              0












                              0








                              0







                              The term ad-hoc is used to refer to queries that have only been executed once. This is consistent with the definition used for the database setting "Optimize for ad-hoc workloads".



                              The link you reference regards deleting ad-hoc queries includes this specific definition "Delete ad-hoc queries: This deletes the queries that were only executed only once and that are more than 24 hours old."






                              share|improve this answer













                              The term ad-hoc is used to refer to queries that have only been executed once. This is consistent with the definition used for the database setting "Optimize for ad-hoc workloads".



                              The link you reference regards deleting ad-hoc queries includes this specific definition "Delete ad-hoc queries: This deletes the queries that were only executed only once and that are more than 24 hours old."







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Oct 11 '17 at 13:49









                              Matthew McGiffenMatthew McGiffen

                              14818




                              14818























                                  0














                                  Ad-hoc from sys.dm_exec_cached_plans dmv's objtype column in docs.microsoft.com.




                                  Adhoc: Ad hoc query. Refers to Transact-SQL submitted as language events by using osql or sqlcmd instead of as remote procedure calls.




                                  One use of this column is to see in case of Queries with multiple plans to see if the multiple plans are caused by Adhoc.



                                      SELECT  DISTINCT 
                                  QCP.objtype
                                  ,MultipleQ.PlanCount
                                  ,qStat.query_hash
                                  ,sText.text AS QueryText
                                  FROM (
                                  SELECT query_hash,
                                  COUNT(query_hash) AS PlanCount
                                  FROM sys.dm_exec_query_stats
                                  GROUP BY query_hash
                                  ) AS MultipleQ
                                  INNER JOIN sys.dm_exec_query_stats qStat ON MultipleQ.query_hash = qStat.query_hash
                                  INNER JOIN sys.dm_exec_cached_plans QCP
                                  ON QCP.plan_handle = qStat.plan_handle
                                  CROSS APPLY sys.dm_exec_sql_text(qStat.sql_handle) AS sText
                                  CROSS APPLY sys.dm_exec_query_plan(qStat.plan_handle) AS qp
                                  WHERE PlanCount > 1
                                  ORDER BY MultipleQ.PlanCount DESC





                                  share|improve this answer






























                                    0














                                    Ad-hoc from sys.dm_exec_cached_plans dmv's objtype column in docs.microsoft.com.




                                    Adhoc: Ad hoc query. Refers to Transact-SQL submitted as language events by using osql or sqlcmd instead of as remote procedure calls.




                                    One use of this column is to see in case of Queries with multiple plans to see if the multiple plans are caused by Adhoc.



                                        SELECT  DISTINCT 
                                    QCP.objtype
                                    ,MultipleQ.PlanCount
                                    ,qStat.query_hash
                                    ,sText.text AS QueryText
                                    FROM (
                                    SELECT query_hash,
                                    COUNT(query_hash) AS PlanCount
                                    FROM sys.dm_exec_query_stats
                                    GROUP BY query_hash
                                    ) AS MultipleQ
                                    INNER JOIN sys.dm_exec_query_stats qStat ON MultipleQ.query_hash = qStat.query_hash
                                    INNER JOIN sys.dm_exec_cached_plans QCP
                                    ON QCP.plan_handle = qStat.plan_handle
                                    CROSS APPLY sys.dm_exec_sql_text(qStat.sql_handle) AS sText
                                    CROSS APPLY sys.dm_exec_query_plan(qStat.plan_handle) AS qp
                                    WHERE PlanCount > 1
                                    ORDER BY MultipleQ.PlanCount DESC





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      Ad-hoc from sys.dm_exec_cached_plans dmv's objtype column in docs.microsoft.com.




                                      Adhoc: Ad hoc query. Refers to Transact-SQL submitted as language events by using osql or sqlcmd instead of as remote procedure calls.




                                      One use of this column is to see in case of Queries with multiple plans to see if the multiple plans are caused by Adhoc.



                                          SELECT  DISTINCT 
                                      QCP.objtype
                                      ,MultipleQ.PlanCount
                                      ,qStat.query_hash
                                      ,sText.text AS QueryText
                                      FROM (
                                      SELECT query_hash,
                                      COUNT(query_hash) AS PlanCount
                                      FROM sys.dm_exec_query_stats
                                      GROUP BY query_hash
                                      ) AS MultipleQ
                                      INNER JOIN sys.dm_exec_query_stats qStat ON MultipleQ.query_hash = qStat.query_hash
                                      INNER JOIN sys.dm_exec_cached_plans QCP
                                      ON QCP.plan_handle = qStat.plan_handle
                                      CROSS APPLY sys.dm_exec_sql_text(qStat.sql_handle) AS sText
                                      CROSS APPLY sys.dm_exec_query_plan(qStat.plan_handle) AS qp
                                      WHERE PlanCount > 1
                                      ORDER BY MultipleQ.PlanCount DESC





                                      share|improve this answer















                                      Ad-hoc from sys.dm_exec_cached_plans dmv's objtype column in docs.microsoft.com.




                                      Adhoc: Ad hoc query. Refers to Transact-SQL submitted as language events by using osql or sqlcmd instead of as remote procedure calls.




                                      One use of this column is to see in case of Queries with multiple plans to see if the multiple plans are caused by Adhoc.



                                          SELECT  DISTINCT 
                                      QCP.objtype
                                      ,MultipleQ.PlanCount
                                      ,qStat.query_hash
                                      ,sText.text AS QueryText
                                      FROM (
                                      SELECT query_hash,
                                      COUNT(query_hash) AS PlanCount
                                      FROM sys.dm_exec_query_stats
                                      GROUP BY query_hash
                                      ) AS MultipleQ
                                      INNER JOIN sys.dm_exec_query_stats qStat ON MultipleQ.query_hash = qStat.query_hash
                                      INNER JOIN sys.dm_exec_cached_plans QCP
                                      ON QCP.plan_handle = qStat.plan_handle
                                      CROSS APPLY sys.dm_exec_sql_text(qStat.sql_handle) AS sText
                                      CROSS APPLY sys.dm_exec_query_plan(qStat.plan_handle) AS qp
                                      WHERE PlanCount > 1
                                      ORDER BY MultipleQ.PlanCount DESC






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 30 mins ago

























                                      answered 1 hour ago









                                      LijoLijo

                                      3701420




                                      3701420






























                                          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%2f188203%2fsql-server-query-store-what-is-considered-an-ad-hoc-query%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

                                          الفوسفات في المغرب

                                          Four equal circles intersect: What is the area of the small shaded portion and its height

                                          جامعة ليفربول