Advanced SQL Memory Management (GeekReady 2012)
-
Upload
fabricio-catae -
Category
Technology
-
view
118 -
download
0
Transcript of Advanced SQL Memory Management (GeekReady 2012)
Fabricio CataeAdvanced Memory
Agenda: SQL MemoryServer Memory
The concept of Committed Memory
Memory Manager Distribute and Balance the memory among the processes
StrategyUse Page File for pages rarely usedUse RAM for pages frequently used
Windows Memory Manager
Physical Memory
Monitoring (Windows)
ProcessC.exe
ProcessA.exeProcessB.ex
e
ProcessD.exe
Physical Memory
Monitoring (Windows)
ProcessC.exe
ProcessA.exe
ProcessB.exe
ProcessD.exe
Committed Memory = RAM
Working Set(100%)
Working Set (30%)
Memory paged out(Pagefile)
Physical Memory
SQL Server Response
ProcessC.exe
ProcessA.exe
SQLSERVR.EXE
(SQL Server)
ProcessD.exe
Committed Memory = RAM
Working Set(100%)
Working Set (30%)
This is BAD!!!
A significant part of sql server process memory has been paged out. This may result in a performance degradation. Duration: 315 seconds. Working set (KB): 660338, committed (KB): 2201296, memory utilization: 30%.
http://support.microsoft.com/kb/918483
Maximize Throughput for Network Applications
Windows Memory Manager prefers trimming the System Cache prior to the Processes
Lock Pages in MemoryAllows SQL Server to allocate physical memoryEnable AWE – Use direct memory allocationLarge Pages – Use 64-kb block size (TLB cache)Windows cannot use the Page File
Windows Settings
In order to achieve maximum performance, SQL Server memory manager tries to use all the available RAM
SQL Memory UsageGrows if there is “Available Memory”Shrinks if the OS indicates “Low Memory”
Total memory is controlled using a Target
SQL Server Memory Manager
Target
Physical Memory
SQL Server uses all Available MemoryTarget Memory
ProcessC.exe
ProcessA.exeSQL Server
ProcessD.exe
Target
SQL Server
Database Cache
Access Manager
Buffer Manager
Memory Disk
Allocates the memory per NUMA Node
What is NUMA?
Buffer Manager
Hardware Performance BottleneckSymmetric Multiprocessor (SMP)
Node(MemoryNod
e)
Design solution for hardware scaling
MemoryLocalForeign
Non-Uniform Memory Access (NUMA)
Allocates the memory per NUMA Node
SQL Server organizes the buffers and tries to affinitize the tasks with the local memory
Fast = Local MemorySlow = Foreign memory
Foreign access is still faster than Disk times
Buffer Manager
Allocated memory from SPA is called “Stolen”
Provides an internal method for memory allocation (limited to 8Kb)
Example:
Single Page Allocator (SPA)
SELECT type, memory_node_id, single_pages_kbFROM sys.dm_os_memory_clerks
Buffer UsageDatabase cacheSingle Page allocatorMinimum Free pages
Buffer Manager Distribution
DBCC MEMORYSTATUS
Who stole my memory?Server Memory
(Physical Memory)
Single Page Allocator
Multi Page Allocator
Virtual Allocator
Shared Allocator
MemClerkMemClerkMemClerkMemClerkMemClerk
Compilation Procedure Cache
Allocators
MemClerk
MemClerk
MemClerk
MemClerk
SPA MPA VM SMSingle Page Allocator
Multi Page Allocator
Virtual Memory
Sharedsys.dm_os_memory_clerks
Stolen “Memory To Leave” (MTL)
Every Memory Clerk reportsVirtual Memory (VM)Shared Memory (SM) SinglePage allocatorMultiPage allocator
DBCC MEMORYSTATUS
DBCC MEMORYSTATUS
CACHESTORE_SQLCPChanges in Caching Behavior between SQL Server 2000, SQL Server 2005 RTM and SQL Server 2005 SP2http://blogs.msdn.com/b/sqlprogrammability/archive/2007/01/22/3-0-changes-in-caching-behavior-between-sql-server-2000-sql-server-2005-rtm-and-sql-server-2005-sp2.aspx
USERSTORE_TOKENPERMA gradual increase in memory consumption for the USERSTORE_TOKENPERM cache store occurs in SQL Server 2005http://support.microsoft.com/kb/933564
USERSTORE_SCHEMAMANAGERMemory consumption in MEMORYCLERK_SOSNODE and in USERSTORE_SCHEMAMANAGER may cause a performance slowdown of a SQL Server 2008 databasehttp://support.microsoft.com/kb/959767
Known Issues
Balance the memoryCompilation and OptimizationTask ExecutionGeneral Caching mechanismDatabase Cache
All memory allocation is reported into one specific Memory Clerk. This allows finding which resource is consuming the resources.
Memory Contention
Stolen Memory
DBCC MemoryStatus (Compilation)
Limits the number of concurrent compilation
Very LARGE COMPILATION = 1Simple query = 4 queries per CPU
If the number of compilation exceeds the limit, then the following tasks are suspended
RESOURCE_SEMAPHORE_QUERY_COMPILE
Compilation Gateways
DBCC MemoryStatus (Workspace)
Session memory allocated for query execution in order to process
HashSortParallelism
Limits the number of concurrent query execution
RESOURCE_SEMAPHORE
Workspace Memory
TypesGeneric ClerksSpecialized Caches
User StoreCache Store
Object Pools
Examples:MEMORYCLERK_SQLCLRUSERSTORE_OBJPERMCACHESTORE_OBJCPCACHESTORE_SQLOBJECTSTORE_SNI_PACKET
Memory Clerks
Types (sys.dm_os_memory_clerks)Generic ClerksSpecialized Caches
User Store (sys.dm_os_memory_cache_counters)Cache Store (sys.dm_os_memory_cache_hash_tables)
Object Pools (sys.dm_os_memory_pools)
Examples:MEMORYCLERK_SQLCLRUSERSTORE_OBJPERMCACHESTORE_OBJCPCACHESTORE_SQLOBJECTSTORE_SNI_PACKET
Memory Clerks (Caches)
Who stole my memory?Server Memory
(Physical Memory)
Single Page Allocator
Multi Page Allocator
Virtual Allocator
Shared Allocator
Buffer PoolStolen
Single Page Allocator
“MemToLeave”
MemClerkMemClerkMemClerkMemClerkMemClerk
Who stole my memory?Server Memory
(Physical Memory)
Single Page Allocator
Multi Page Allocator
Virtual Allocator
Shared Allocator
MemClerkMemClerkMemClerkMemClerkMemClerk
Procedure Cache SQL CLR
Buffer PoolStolen
Single Page Allocator
“MemToLeave”
AgendaSQL Memory TopicsSQL Latches
SQL OSMemory
Management
Thread Scheduling
Rubén GonzálezLatches
A device for holding a door, gate, or the like, closed, consisting basically of a bar falling or sliding into a catch, groove, hole, etc.
In SQL Server a latch is a flag that protects a memory structure that could be accessed at the same time by other thread.
What is a Latch?
Was introduced in SQL 7.0 as a consecuence of Row Level LockingYou can’t control latches (no latch hint)Latchs are at memory structure level not at row level. There are:
Buffer Latches IO related Not IO related
Transaction Latches Other Latches
Latch facts
Factors under your control can affect
visibility and behavior
Why do I care?You will see latched
in…
Your Application
Your Design and Configuration
Throughput of IO Subsystem
sys.dm_os_wait_stats
sys.dm_os_latch_stats
sys.dm_exec_requests
sys.dm_os_waiting_tasks
XEventsPerformance
CountersError (Latch
Timeout)
OTHER LATCHLATCH_NLLATCH_KPLATCH_SHLATCH_UPLATCH_EXLATCH_DT
Braekdown of Latch WaitsBUFFER LATCH
PAGELATCH_NLPAGELATCH_KPPAGELATCH_SHPAGELATCH_UPPAGELATCH_EXPAGELATCH_DTPAGEIOLATCH_NLPAGEIOLATCH_KPPAGEIOLATCH_SHPAGEIOLATCH_UPPAGEIOLATCH_EXPAGEIOLATCH_DT
TRANSACTION LATCHTRAN_MARKLATCH_NLTRAN_MARKLATCH_KPTRAN_MARKLATCH_SHTRAN_MARKLATCH_UPTRAN_MARKLATCH_EXTRAN_MARKLATCH_DT
Used for synchronization of
commits with marked
transactions
The breakdown of these is in
sys.dm_os_latch_stats
+0
Ok, but what is it really?
+8+16+24 class
countownerwaiters List of Waiters
Task Address of Owner
This is the latch
Latch Class (latch id)
Num KP Stuff Spin Wait DT EX UPNum
SH
0123453563 111236
Count of KP Latches
Count of SH Latches
Superlatch and other stuff
spinlock Waiters exist
DT Latch held
EX Latch held
UP Latch held
x64 C++ Class
Just reading. No ownership tracking
Readers are ok but no writes
It’s all mine, but I’m tracked
Destroy latch; Heaviest of all. Ex. Removing buffer from
cache.
Keep latch; Very light; just keeping reference count
Latch ConcurrencyKP S
HUP
EX DT
KP Y Y Y Y NSH
Y Y Y N N
UP
Y Y N N N
EX Y N N N NDT N N N N N
Y turns N if
waiters
Mode
Not always FIFO
KP
SH
UP
EX
DT
The BUFFER (BUF) latchBUF
SH for Reading
UP and EX for writing
KP for traversing
DT for freeing
PAGELATCH_XX or
PAGEIOLATCH_XX
Buffer latch in AM
PAGE (in cache)
BUF_ONLRU
0x000001
BUF_DIRTY 0x000002BUF_IO 0x000004BUF_HASHED
0x000008
BUF_READ 0x000400BUF_IOERR 0x000800
bpagebpageno
bstat
blatch
blog
m_headerversion
m_page_id
ptr
value
Latch
Class
Why not just reuse the BUF lacth concept?
Shows up as LATCH_XX in DMVs
Wait_resource is the type (class) and mem address of latch
Sys.dm_os_latch_stats to see all 144 of them
Other wait types are not latches (Ex. ASYNC_NETWORK_IO, CX_PACKET, ...)
Let’s talk Non-BUF Latches
Why should you care?
Normally you
wouldn’t because…
Many of the 144
classes you will never
see
But what if thousands
of waits with high avg wait
time
Or the latch shows up as a big waiter or blocker in
your monitoring
Or lot of waits that cumulative
slowing down a query
Nesting Transactions: RCSI and parallel queriesFGCB_ADD_REMOVE: Autogrow of Data FilesLOG_MANAGER: Autogrow of tlogACCESS_METHODS_DATASET_PARENT: Only seen during parallel queriesALLOC_FREESPACE_CACHE: Partition your tableALLOC_EXTENT_CACHE: Partition your tableTRANSACTION_DISTRIBUTED_MARK: Also appears as TRAN_MARKLATCH. Distributed Transactions.
Ones to look out for
Could be an issue when in the seconds rangeNot always a high count for a problem
Are latched causing you problems
PAGEIOLATCH waits = I/O Bottleneck
High BUF latch mean hot pages
Non-BUF latch waits depend on the latch class
Latch Timeouts
What is the avg disk
transfer rate
Unless a bug it is usually allocation pages (and usually tempdb)Could be system table pages for a DDL
Find out the “hot” class and see if one of generally know issues
This is just bad no matter what type (5 minute wait!)
Check sys.dm_db_index_operational_stats
© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.