Advanced SQL Memory Management (GeekReady 2012)
-
Upload
fabricio-catae -
Category
Technology
-
view
121 -
download
0
Transcript of Advanced SQL Memory Management (GeekReady 2012)
![Page 1: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/1.jpg)
Fabricio CataeAdvanced Memory
![Page 2: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/2.jpg)
Agenda: SQL MemoryServer Memory
![Page 3: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/3.jpg)
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
![Page 4: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/4.jpg)
Physical Memory
Monitoring (Windows)
ProcessC.exe
ProcessA.exeProcessB.ex
e
ProcessD.exe
![Page 5: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/5.jpg)
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)
![Page 6: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/6.jpg)
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
![Page 7: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/7.jpg)
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
![Page 8: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/8.jpg)
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
![Page 9: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/9.jpg)
Target
Physical Memory
SQL Server uses all Available MemoryTarget Memory
ProcessC.exe
ProcessA.exeSQL Server
ProcessD.exe
Target
SQL Server
![Page 10: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/10.jpg)
Database Cache
Access Manager
Buffer Manager
Memory Disk
![Page 11: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/11.jpg)
Allocates the memory per NUMA Node
What is NUMA?
Buffer Manager
![Page 12: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/12.jpg)
Hardware Performance BottleneckSymmetric Multiprocessor (SMP)
![Page 13: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/13.jpg)
Node(MemoryNod
e)
Design solution for hardware scaling
MemoryLocalForeign
Non-Uniform Memory Access (NUMA)
![Page 14: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/14.jpg)
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
![Page 15: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/15.jpg)
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
![Page 16: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/16.jpg)
Buffer UsageDatabase cacheSingle Page allocatorMinimum Free pages
Buffer Manager Distribution
DBCC MEMORYSTATUS
![Page 17: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/17.jpg)
Who stole my memory?Server Memory
(Physical Memory)
Single Page Allocator
Multi Page Allocator
Virtual Allocator
Shared Allocator
MemClerkMemClerkMemClerkMemClerkMemClerk
Compilation Procedure Cache
![Page 18: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/18.jpg)
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)
![Page 19: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/19.jpg)
Every Memory Clerk reportsVirtual Memory (VM)Shared Memory (SM) SinglePage allocatorMultiPage allocator
DBCC MEMORYSTATUS
![Page 20: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/20.jpg)
DBCC MEMORYSTATUS
![Page 21: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/21.jpg)
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
![Page 22: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/22.jpg)
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
![Page 23: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/23.jpg)
DBCC MemoryStatus (Compilation)
![Page 24: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/24.jpg)
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
![Page 25: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/25.jpg)
DBCC MemoryStatus (Workspace)
![Page 26: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/26.jpg)
Session memory allocated for query execution in order to process
HashSortParallelism
Limits the number of concurrent query execution
RESOURCE_SEMAPHORE
Workspace Memory
![Page 27: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/27.jpg)
TypesGeneric ClerksSpecialized Caches
User StoreCache Store
Object Pools
Examples:MEMORYCLERK_SQLCLRUSERSTORE_OBJPERMCACHESTORE_OBJCPCACHESTORE_SQLOBJECTSTORE_SNI_PACKET
Memory Clerks
![Page 28: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/28.jpg)
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)
![Page 29: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/29.jpg)
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
![Page 30: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/30.jpg)
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”
![Page 31: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/31.jpg)
AgendaSQL Memory TopicsSQL Latches
SQL OSMemory
Management
Thread Scheduling
![Page 32: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/32.jpg)
Rubén GonzálezLatches
![Page 33: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/33.jpg)
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?
![Page 34: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/34.jpg)
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
![Page 35: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/35.jpg)
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)
![Page 36: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/36.jpg)
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
![Page 37: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/37.jpg)
+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
![Page 38: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/38.jpg)
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
![Page 39: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/39.jpg)
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
![Page 40: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/40.jpg)
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
![Page 41: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/41.jpg)
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
![Page 42: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/42.jpg)
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
![Page 43: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/43.jpg)
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
![Page 44: Advanced SQL Memory Management (GeekReady 2012)](https://reader035.fdocuments.in/reader035/viewer/2022062412/58ab78961a28abb54e8b69c7/html5/thumbnails/44.jpg)
© 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.