(Translated) Memory meditation: multiple names associated with the mysterious SQL Server memory consumers.

Original Source: https://blogs.msdn.microsoft.com/sqlmeditation/2013/01/01/memory-meditation-the-mysterious-sql-server-memory-consumer-with-many-names/

 

 

For the names of a number of different memory consumers

Have you ever wondered what memory is granted (Memory grants)?
What is reserved (scheduled) memory (QE Reservations) query execution?
And query execution memory (Query Execution Memory)?
Workspace Memory (Workspace memory), and memory retention (Translator's Note: Many places will Reservations translated as "reserved", personally I think that translated as "scheduled" time easier to understand, below a predetermined synonymous with "reservations")


Like the same life in many things, in short, in a word: so the names are pointing in the same SQL Server memory consumers,
in order to sort or hash operations (including bulk copy, and index creation, etc.) during inquiry memory allocation
, please allow me to arise out of a larger topic during the execution of a query, the query may allocate memory from a different "buckets" or clerks, the (difference from different parts of the application) depends on the application memory used to do.
For example, a query is parsed and compiled the first time, he needs to compile or optimize memory consumption. Once the query is compiled, the application's memory he will be released,
while the corresponding query execution plan is cached, for the implementation of this plan will be spending the procedure cache memory and this memory has been occupied until the server is restarted or memory pressure occurs at this time, ready to execute the query.
If ordering or hashing the query occurs (or a combination of connection), it is preferably to use to achieve (a result) of a predetermined memory storage for storing the result or Hash bucket
this part of the memory occurs during query execution, it would involve a lot of specific name of the memory.


The terms and troubleshooting tools

Let us recall the memory consumption of different categories on this person that you may encounter.
Similarly, all of which describe the concepts related to the same memory allocation:

Query Execution memory (QE Memory):
  this one is used to emphasize the sort / hash memory to use this fact during query execution, the query is also part of the largest memory consumption during the execution.

Query memory (QE) scheduled or memory book:
  When a query needs to sort / hash operational readiness memory when, during execution, he will be based include the sort or hash operation of the original query plan, the implementation of a memory reservation request (memory reservation request ),
  then the query began execution, he requested memory, while sqlserver will grant the query part or all of the memory requests, mainly dependent on available memory server
  has called MEMORYCLERK_SQLQERESERVATIONS memory clerk, the memory counters to preserve this part of the memory case allocation.

  Translator's Note ------------------------------ ----------------- ----------------
  this memory can be queried from sys.dm_os_memory_clerks out, MEMORYCLERK_SQLQERESERVATIONS allocation on the server screenshot below is a server load exists
  showing that this part of the memory is still not small , there are more than 4GB MEMORYCLERK_SQLQERESERVATIONS (server memory 32GB)
  If a server does not load when the memory usage is very low, or even 0

  

  --------------------------------------------------------------------------

Memory grant (Memory Grants):
  When SQL Server to a request to grant the request of memory, called a memory grant occurs,
  there is a performance counter retains much memory has granted the request of memory: Memory Grants Outstanding
  another counter indicates that there how many inquiries have been requested sort / hash memory, but waiting for the state, because query execution has run out of memory: memory grants Pending
  these two memory counter shows a lack of memory and memory grant awarded, that is, a single query It may consume 4GB of memory to perform a sort, but in both cases will not be reflected

  Translator's Note ------------------------------ ----------------- ----------------
  this memory can be understood as a request real-time memory, SQL Server real-time request more, this part of the larger memory,
  such as a Session 20MB of memory required to run at the same time operation requires 200MB Session 10, 100 run Session 2000MB need, of course, need to run each Session not the same memory (memory grant), only for example herein
  can be queried from sys.dm_os_memory_clerks, the following is a screenshot MEMORYCLERK_SQLQERESERVATIONS distribution of the load on the server, the server exists
  showing that this part of the memory is not small, there are more than 4GB MEMORYCLERK_SQLQERESERVATIONS (server memory 32GB)
  If a server does not load when the memory usage is very low, even can be 0
  memory Grants Outstanding Session can not be certain of its application memory, in a wait state memory,
  SQL Server is a self-regulating (self tuning) engine, this memory will generally reserved enough, normal In the case because of lack of memory to perform sql waiting does not occur
  in all things clear priorities score, divided into the same memory needed and requested elective, there can not occupy memory to cache data, real-time and can not even here respond to it.
  To give an example of inappropriate, no matter how poor, can not buy a house without a car, the total leave enough money to eat it.
  Memory may exist elsewhere in the pressure (such as data cache may quickly be replaced out of memory), but the face of the corresponding memory request real-time, SQL Server remain quite adequate, rarely seen because of personal memory and can not wait to perform Session Case.
  But words can not turn, can not say no Memory Grants Pending, it means no memory pressure.

  

  ----------------------------------------------------------------------------

In order to observe individual requests and memory they have applied, you can check the DMV sys.dm_exec_query_memory_grants
this DMV shows currently running query memory grant situation, rather than historical circumstances
Alternatively, you can capture the actual query execution plan, inquire about their XML Query plan called a node, it contains the memory grant size,
the following example:
<QueryPlan DegreeOfParallelism = ". 8" MemoryGrant = "2,009,216"
another is the sys.dm_exec_requests DMV, containing the 8kb called granted_query_memory column units ,
such as a value of 1000, which means 1000 * 8kb, or is 8000kb of memory grant

Workspace Memory (Workspace Memory):

  It is still another memory describe the same item,
  you will often see the performance of this technology area granted Workspace Memory (KB), reflects the current memory usage for all sort / hash operation
  maximum workspace memory (Maximum Workspace Memory ( KB)) is the number of SQL Server since the start of the largest workspace memory.
  In my opinion, the project is a work memory space used to describe a legacy of memory allocation sqlserver7.0 a sqlserver2000 has been canceled after SQLServer2005

Resource semaphore (Resource Semaphore):
  In order for this concept to add more complexity, SQL Server uses thread synchronization object called a semaphore (semaphore) to keep track of how many have been granted a memory.
  The idea is this: If you run out of sqlserver workspace memory / QE memory, then the use of out-of-memory error instead of query execution failed (not direct feedback query failed, but waiting for memory)
  it will lead to the query wait available memory and (after obtaining the available memory) can be re-run

  In this process, Memory Grants Pending the performance counter starts to make sense.
  So it will generate wait_time_ms in sys.dm_exec_query_memory_grants in, granted_memory_kb = NULL, timeout_sec
  the way, and this is the only compiler memory in SQL Server under low memory conditions, will perform two memory waiting for memory.
  In other cases, the query will fail and report directly to the 701 error - not enough memory.

Another waiting in SQL Server type also shows a query is waiting for memory grants --RESOURCE_SEMAPHORE
As stated in the document, this happens, a query memory can not be granted immediately due to other concurrent query execution.
Frequent wait and wait times may indicate that too many concurrent queries, or excessive memory grant amount.
You can observe Session-level wait sys.dm_exec_requests in this DMW

 

Why we need to focus on Memory Grants or Workspace Memory or Query Execution Memory or no matter how you call it.

Over the past few years for diagnosis of performance problems, I found this to be one of the most common memory-related problems, the application looks quite regularly perform some simple query,
but because of the large number of execution Sort or hash operations suffered a lot performance destroy
those queries not only consume a lot of memory during execution, and can lead to other queries waiting for memory, this is the performance bottleneck.

The possibility of using tools (DWVs, performance counters, and the actual execution plan) I provided above, you can check out which query is consuming a lot of memory, then consider optimization / rewrite

Sort / Hash operation profiles generated

Mentioned query rewrite, here are some likely to lead to a lot of memory granted case

Causes (including but not limited to the following cases) sort operations
  the ORDER BY (the SQL-T)
  the GROUP BY (the SQL-T)
  the DISTINCT (the SQL-T)
  the Merge the Join operation is chosen by the optimizer, wherein a combined input coupled It must be ordered


Match the causes Hash operation (including but not limited to the following cases)
  the JOIN (T-SQL) - If you choose to perform a Hash operation SQL, is the lack of a typically expensive join operations --hash join index reasonable result, the observation plan
  dISTINCT (T-SQL) -Hash polymerization may be used to perform distinct operations, the observation plan
  SUM / AVG / MAX / MIN ( T-SQL) - any polymeric Hash operation may cause the polymerization, the observation program
  UNION - Hash polymerization may be used for removing duplicate items


Understanding these common causes can help application developers to eliminate as much as possible to grant a request for a large amount of memory SQL Server.
As always, basic query tuning begin to examine the query whether there are appropriate index to help reduce them to read, as much as possible to reduce or eliminate large sorts.

Here is a memory grant blog for reference http://blogs.msdn.com/b/sqlqueryprocessing/archive/2010/02/16/understanding-sql-server-memory-grant.aspx

 

Guess you like

Origin www.cnblogs.com/VicLiu/p/11654640.html