Query hash vs statement_sql_handle

sakuraime 2,341 Reputation points
2021-06-09T04:36:22.27+00:00

there two columns from sys.dm_exec_requests call query_hash, and statement_sql_handle
from sys.dm_exec_query_stats also has these two columns .

may I know what's the actual difference from them ?

and also , from my server , I see a lot of entry that have query_hash , but not statement_sql_handle . why is that ?

Thanks

SQL Server Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2021-06-09T08:41:35.143+00:00

    Hi @sakuraime ,

    query_hash

    Binary hash value calculated on the query and used to identify queries with similar logic. You can use the query hash to determine the aggregate resource usage for queries that differ only by literal values.

    A query_hash is a computed value that points to a query irrespective of literal values.

    statement_sql_handle

    SQL handle of the individual query. This column is NULL if Query Store is not enabled for the database.

    Suggest you read this bog What is a query_hash or query_plan_hash and why is it useful to better understand this.


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.


  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-06-09T22:10:16.873+00:00

    may I know what's the actual difference from them ?

    Well, one is longer than the other...

    The SQL handle is an internal identifier and serves as an address in some DMVs, for instance sys.dm_sql_exec_text.

    The query_hash is computed from the "shape" of the query. Two queries that only differs in literal values have the same hash. When you analyse data, for instance in Query Store, queries that have the same query hash can be considered the same from a performance tuning perspective. The query_hash is only an aide for DBAs and similar, but not used internally.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.