Micrsft SQL Server Administratin Disk & memry David Hksza http://siret.cz/hksza
Lecture Outline Query lifecycle Data strage SQL Server Memry
SELECT Query Lifecycle 1. SNI client-server cnnectin using TCP/IP (r ther prtcl) 2. SELECT statement using TDS (Tabular Data Streams) messages between TDS endpints 3. Unwrapping TDS message 4. Sending SQL Cmmand t Cmmand Parser 5. Checking Plan Cache in the Buffer Pl 6. If the plan is nt cached, passing a query tree t the Optimizer 7. Query plan passed t Query Executr 8. Passing Query Plan t Access Methds (AM) 9. Checking the existence f the required page in the Data Cache by the Buffer Manager 10. Passing the requested page(s) back t the AM 11. Passing the results set t the Relatinal Engine SQLOS credit: SQL Server 2008 Internals and Trubleshting
UDPATE Query Lifecycle 1. Identificatin f the page t update as in the SELECT lifecycle 2. The Access Methds require Write-Ahead Lgging frm the Lg Manager (part f the Transactin Manager) 3. Page changes stred in the Transactin Lg 4. The AM receive cnfirmatin and pass the request n t the Buffer Manager 5. Mdificatin f the page in cache 6. Cnfirmatin sent t the AM and the client SQLOS credit: SQL Server 2008 Internals and Trubleshting
Buffer Manager (1) Manages disk I/O functins fr bringing data and index pages int the data cache Buffer 8KB memry page all memry nt used by ther cmpnents stred in the buffer pl Free buffer list lw free buffer list lazywriter prcess Dirty pages pages mdified in cache but nt immediately written t disk durability maintained by the transactin lg flushing dirty pages wrker thread when the free buffer list is lw checkpint DBCC DROPCLEANBUFFERS flush clean pages frm cache suitable fr testing purpses sys.dm_s_buffer_descriptrs is_mdified
Buffer Manager (2) Lazywriter prcess prcess which peridically checks the size f the free buffer list ages-ut lng enugh nt used pages releases memry t OS Checkpint prcess dirty pages f cmmitted transactins are written t disk des nt remve pages frm cache ccurs autmatically r using CHECKPOINT cmmand trace flag 3502 prvides lgging - DBCC TRACEON(3502, -1) DBCC TRACEOFF(3502, -1) checkpint; EXEC xp_readerrrlg BM state can be checked using the sys.dm_s_perfrmance_cunters view
Pages 8KB header rws rw ffsets Allcatin units IN_ROW_DATA (hbt - Heap Or B-Tree) majrity f data LOB_DATA (LOB) TEXT, NTEXT, IMAGE, stred ut-rw sp_tableptin text in rw ROW_OVERFLOW_DATA (SLOB - Small-LOB) VARCHAR, VARBINARY,.NET, stred in-rw unless the rw exceeds 8KB sys.allcatin_units
Extent 8 pages 64KB Types unifrm mixed First page f a new table mixed extent New index large unifrm extents small mixed extents
Space Management (1) Glbal Allcatin Map (GAM) 1 = free extent, 0 = allcated extent cvers 64,000 extents Extent status GAM bit setting SGAM bit setting Shared Glbal Allcatin Map (SGAM) cvers 64,000 extents 1 = extent is being used as a mixed extent and has a free page, 0 = extent is nt used as a mixed extent, r it is a mixed extent and all its pages are being used Free 1 Unifrm extent (allcated) Mixed extent with free pages Full mixed extent 0 0 0 1 0 0 Page Free Space (PFS) allcatin status f each page 1B
Space Management (2) Index Allcatin Map (IAM) tracking extents in 4GB used by an allcatin unit can be linked IAM chain maps space allcatin fr heaps and b-trees LOB data rw-verflw data IAM page 96-byte page header cntaining inf abut what part f space the IAM maps IAM page header (8 page-pinter slts) bitmap fr extents belnging t the allcatin unit sys.system_internals_allcatin_units Inserting a new rw IAM extents fr the allcatin unit PFS free pages in identified extents
Database File Structure
Memry 32 bit system 4GB address space 2GB user mde 2GB kernel mde 4GB tuning allws t use 3GB user mde address space Windws Server 2003 /3GB in bt.ini Windws Server 2008 BCDEdit /set increaseuserva 3072 applicatin needs t be linked with /LARGEADDRESSAWARE nnpaged pl, paged pl and system PTEs (Page Table Entry) need t be mnitred Physical Address Extensin (PAE) intrduced by Intel address bus = 36 bits 64GB Windws Enterprise Windws Server 2003 /PAE in bt.ini Windws Server 2008 bcdedit /set [{ID}] pae FrceEnable applicatins must be written t be able t use AWE (Address Windwing Extensins) which allws a prcess t access memry utside f its VAS (by mapping this memry int VAS) PAE must be enabled in SQL Server by sp_cnfigure r SSMS SQL Server service needs t have Lck Pages in Memry privilege explitable by data cache nly
Memry infrmatin and rerestrictin Memry size restrictin min server memry max server memry sp_cnfigure 'max server memry', 4096; sys.dm_s_sys_inf infrmatin abut the cmputer n which SQL Server is installed including the resurces available t and cnsumed cmmitted_kb cmmitted physical memry in the memry manager cmmit_target_kb memry needed by the memry manager
Memry Architecture Memry ndes Memry divisin At least ne nde depending n using the NUMA (Nn-Unifrm Memry Access) architecture SELECT DISTINCT memry_nde_id FROM sys.dm_s_memry_clerks Memry allcatrs Memry allcatin n the memry ndes g thrugh memry allcatr rutines tied t the memry ndes Page allcatrs, Virutal memry allcatr, Shared memry allcatr Memry clerks each cnsumer allcates memry thrugh a memry clerk Can be used t track memry usage by cmpnents sys.dm_s_memry_clerks Memry brker centralized mechanism t distribute memry r cntrl the allcatins made by each cmpnent in SQL Server mnitrs the demand cnsumptin f memry by each cmpnent
Memry clerks Clerk types (cmmn caching mechanism implemented by SQLOS) bject stre simple stre, hmgeneus data (SNI pling netwrk buffers) cache stre SQLOS management f life time and visibility cntrl (plan cache) user stre cache stre + strage semantics (metadata cache) different stres use different replacing strategies and csting mechanisms
Cache sys.dm_s_memry_cache_cunters Caches are clerks Data Cache largest cache in the buffer pl sys.dm_s_buffer_descriptrs Plan Cache caching f executin plans sys.dm_exec_cached_plans DBCC FREESYSTEMCACHE (DBCC FREESYSTEMCACHE ('SQL Plans'))
Mnitring Memry Perfrmance mnitr (perfmn) DMVs useful cunters Memry bject Prcess bject Paging file SQL Server:Buffer Manager (MSSQL$NDBI039:Buffer Manager) bject sys.dm_s_perfrmance_cunters sys.dm_s_memry_clerks sys.dm_s_prcess_memry DBCC MEMORYSTATUS https://supprt.micrsft.cm/kb/907877/en-us SQL Server Prfiler
Tasks 1. Identify names f 10 bjects with the highest number f data pages (sys.allcatin_units, sys.partitins, OBJECT_NAME) 2. Create a table with rws exceeding page size and check hw ROW_OVERFLOW_DATA allcatin units are used (sys.allcatin_units, REPLICATE) 3. Create a stred prcedure returning the number f recrds in a table based n the sys.partitins view. 4. Find ut the number f dirty pages in each database n the server (verify by using the CHECKPOINT cmmand) (sys.dm_s_buffer_descriptrs). 5. Is a page set t dirty if an UPDATE changes a clumn t the same value (UPDATE t SET cl = cl)?